2014-12-03 45 views
0

我有一个像下面的实现,它返回js创建动态webview shouldInterceptRequest方法,但我需要做一个请求int int,并在返回inputStream之前等待它的答案。我如何让它等待这个。我怎样才能在同步方法中使用新线程

我可以准备和同步请求这个,它解决了这个问题,但我只是想知道有一种方法可以在新线程中做到这一点。

MRURLProtocol.mapDomain("gamification.fs", new MRProtocolResponseHandler() { 
      @Override 
      public InputStream handle(Uri uri) { 
      InputStream stream = null; 
       myApplication.getChamp().getImpulser().invokeRequest(method, body,new OnWebApiResponseArrived() { 
        @Override 
        public void OnSuccess(Object obj) { 
         String configString = "(function() { if (typeof(window) === 'undefined') ..... %s ...."; 
         configString = String.format(configString, obj); 
         stream = new ByteArrayInputStream(configString.getBytes()); 
        } 

        @Override 
        public void OnFail(String errMsg) { 
         String configString = "(function() { if (typeof(window) === 'undefined')..... %s ...."; 
         configString = String.format(configString, errMsg); 
         stream = new ByteArrayInputStream(configString.getBytes()); 
        } 
       }); 
       // NEED TO WAIT FOR stream before return 

       return stream; 
      } 
     }); 

调用请求(显示它正在开发新的线程)

public void invokeRequest(String entryPoint,String body,final OnWebApiResponseArrived callback){ 
     GameApiRequest request = new GameApiRequest(); 
     request.setEntryPoint(entryPoint); 
     request.setBody(body); 

     request.setUIResponseHandler(new Handler(){ 
      @Override 
      public void handleMessage(Message msg) { 

        if(msg.obj != null){ 
         if(msg.what == MCStatics.ResponseMessageCode) 
          callback.OnSuccess(msg.obj); 
         else 
          callback.OnFail("please,try again later."); 

        } 
       } 

      } 
     }); 

     Thread thread = new Thread(request); 
     thread.start(); 
    } 

回答

0

我用countdownlatch解决了这个问题,

private final CountDownLatch responseLatch = new CountDownLatch (1); 

MRURLProtocol.mapDomain("gamification.fs", new MRProtocolResponseHandler() { 
      @Override 
      public InputStream handle(Uri uri) { 
      InputStream stream = null; 
       myApplication.getChamp().getImpulser().invokeRequest(method, body,new OnWebApiResponseArrived() { 
        @Override 
        public void OnSuccess(Object obj) { 
         String configString = "(function() { if (typeof(window) === 'undefined') ..... %s ...."; 
         configString = String.format(configString, obj); 
         stream = new ByteArrayInputStream(configString.getBytes()); 

         responseLatch.countDown(); 
        } 

        @Override 
        public void OnFail(String errMsg) { 
         String configString = "(function() { if (typeof(window) === 'undefined')..... %s ...."; 
         configString = String.format(configString, errMsg); 
         stream = new ByteArrayInputStream(configString.getBytes()); 

         responseLatch.countDown(); 
        } 
       }); 

       try { 
        responseLatch.await(); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 

       return stream; 
      } 
     });