Java 如何使用新的同步线程方法
我有一个类似于下面的实现,它将动态创建的js返回到webview shouldInterceptRequest方法,但我需要在返回inputStream之前对其进行请求并等待其回答。我怎么能让它等这个呢 我可以准备和同步这个请求,它解决了这个问题,但我只是想知道有没有一种方法可以在新的线程中这样做Java 如何使用新的同步线程方法,java,android,Java,Android,我有一个类似于下面的实现,它将动态创建的js返回到webview shouldInterceptRequest方法,但我需要在返回inputStream之前对其进行请求并等待其回答。我怎么能让它等这个呢 我可以准备和同步这个请求,它解决了这个问题,但我只是想知道有没有一种方法可以在新的线程中这样做 MRURLProtocol.mapDomain("gamification.fs", new MRProtocolResponseHandler() { @Override
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;
}
});
调用请求以显示它正在新线程上工作
我用倒计时锁解决了这个问题
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;
}
});
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;
}
});