I was intercepting AJAX request in cachedResponseForRequest:
, and then construct a custom Response for the request. But it seems that this API method always work on a same thread, so if I was doing a time consuming operation, new requests would be blocked. In Android
, I solved this problem using PipedStream, But cachedResponseForRequest:
returns response with NSData
, How can I make NSData pipable, can anybody help me out
Here is My Code:
- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request
{
NSURL *url=request.URL;
NSCachedURLResponse *response=nil;
if([url.path containsString:@"aa1"]){
NSMutableData *data=[NSMutableData data];
NSThread* asyncThread = [[NSThread alloc] initWithTarget:self selector:@selector(getData:) object:data];
[asyncThread start];
NSURLResponse *urlresponse=[[NSURLResponse alloc] initWithURL:url MIMEType:@"application/json" expectedContentLength:data.length textEncodingName:@"utf-8"];
response=[[NSCachedURLResponse alloc] initWithResponse:urlresponse data:data];
}
return response;
}
-(void)getData:(NSMutableData*)data{
NSMutableString *str=[NSMutableString stringWithString:@""];
for(int i=0;i<1024*1024;i++){
[str appendFormat:@"s"];
}
[data appendData:[str dataUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"%d",data.length);
}
And In android, I solved this matter like follow
public WebResourceResponse shouldInterceptRequest(WebView view,
String url) {
WebResourceResponse response = null;
if (url.contains("loca/aa0")) {
try {
final PipedOutputStream out = new PipedOutputStream();
InputStream in = new PipedInputStream(out);
AsyncTask<String, Integer, String> task = new AsyncTask<String, Integer, String>() {
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
try {
InputStream s = ctx.getAssets().open("s.txt");
byte[] buffer = new byte[100];
while (s.read(buffer) != -1)
out.write(buffer);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
};
task.execute();
response = new WebResourceResponse("application/html","UTF-8", in);
} catch (Exception e) {
e.printStackTrace();
}
}
return response;
}
can anybody see my Question?