In order to get the status code from Android Studio in Webview, I looked at the reference site and wrote the code, but the app fell off in the middle.How can I prevent the app from falling off?
Reference
http://saki0n.blogspot.jp/2013/04/android-webview.html
public class MainActivity extensions AppCompatActivity{
private TextView textView;
@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView= (WebView) findViewById (R.id.webView);
textView=(TextView) findViewById (R.id.textView);
// myWebView.setWebViewClient(newWebViewClient());
myWebView.setWebViewClient(newWebViewClient(){
@TargetApi (Build.VERSION_CODES.HONEYCOMB)
@ Override
public WebResourceResponse shouldInterceptRequest (WebView view, String url) {
if(!url.matches("https?://[\\w\\.\\-]+(/.*)?")) {
return super.shouldInterceptRequest(view, url);
}
HttpGet req = new HttpGet(url);
DefaultHttpClient client=new DefaultHttpClient();
String mimeType=null, encoding=null;
byte [ ] data = null;
try{
HttpResponse res=client.execute(req);
// You can get the status code here!
if(HttpStatus.SC_OK==res.getStatusLine().getStatusCode()){
HttpEntity entity = res.getEntity();
Header mimeHeader=entity.getContentType();
textView.setText(res.getStatusLine().getStatusCode());
if(null!=mimeHeader)mimeType=mimeHeader.getValue();
Header encodingHeader=entity.getContentEncoding();
if(null!=encodingHeader)encoding=encodingHeader.getValue();
data=EntityUtils.toByteArray(entity);
}
} catch(Exceptione){
String msg = e.getMessage();
Log.e(this.getClass().getSimpleName(),(null!=msg)?msg:"");
} US>finally
req.abort();
client.getConnectionManager().shutdown();
}
InputStream stream=new ByteArrayInputStream(data);
return new WebResourceResponse(mimeType, encoding, stream);
}
});
myWebView.loadUrl("https://www.google.com/");
}
}
Excerpt only the relevant parts.
byte[]data=null;
try{
if(HttpStatus.SC_OK==res.getStatusLine().getStatusCode()){
data=EntityUtils.toByteArray(entity);
}
}
InputStream stream=new ByteArrayInputStream(data);
Therefore, except for statusCode200, data==null.
There is no error log, so I can't determine if it's caused by Coco, but I think it will be helpful.
547 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
547 Who developed the "avformat-59.dll" that comes with FFmpeg?
702 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
532 Uncaught (inpromise) Error on Electron: An object could not be cloned
© 2024 OneMinuteCode. All rights reserved.