create an async task for online js parsing
This commit is contained in:
parent
c2634ce266
commit
7fcb1e0874
@ -66,10 +66,6 @@ public class IITC_Mobile extends Activity {
|
||||
// enable progress bar above action bar
|
||||
requestWindowFeature(Window.FEATURE_PROGRESS);
|
||||
|
||||
// TODO build an async task for url.openStream() in IITC_WebViewClient
|
||||
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
|
||||
.permitAll().build();
|
||||
StrictMode.setThreadPolicy(policy);
|
||||
setContentView(R.layout.activity_main);
|
||||
iitc_view = (IITC_WebView) findViewById(R.id.iitc_webview);
|
||||
|
||||
|
@ -17,6 +17,8 @@ import android.webkit.WebView;
|
||||
import android.webkit.WebViewClient;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.cradle.iitc_mobile.async.UrlContentToString;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
@ -24,6 +26,9 @@ import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.Map;
|
||||
import java.util.Scanner;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
public class IITC_WebViewClient extends WebViewClient {
|
||||
private static final ByteArrayInputStream style = new ByteArrayInputStream(
|
||||
@ -90,8 +95,21 @@ public class IITC_WebViewClient extends WebViewClient {
|
||||
// load iitc script from web or asset folder
|
||||
if (iitc_source.contains("http")) {
|
||||
URL url = new URL(iitc_source);
|
||||
js = new Scanner(url.openStream(), "UTF-8").useDelimiter("\\A")
|
||||
.next();
|
||||
// if url.openStream() is parsed from a scanner
|
||||
// the NetworkOnMainThread exception is thrown
|
||||
// use a async task for this
|
||||
try {
|
||||
js = new UrlContentToString().execute(url).get(5, TimeUnit.SECONDS);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
js = this.fileToString("total-conversion-build.user.js", true);
|
||||
} catch (ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
js = this.fileToString("total-conversion-build.user.js", true);
|
||||
} catch (TimeoutException e) {
|
||||
e.printStackTrace();
|
||||
js = this.fileToString("total-conversion-build.user.js", true);
|
||||
}
|
||||
} else {
|
||||
js = this.fileToString("total-conversion-build.user.js", true);
|
||||
}
|
||||
|
@ -0,0 +1,31 @@
|
||||
package com.cradle.iitc_mobile.async;
|
||||
|
||||
import android.os.AsyncTask;
|
||||
|
||||
import com.cradle.iitc_mobile.IITC_Mobile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.Scanner;
|
||||
|
||||
/*
|
||||
* this class parses the content of an web page.
|
||||
* since network operations shouldn't be done on main UI thread
|
||||
* we use an async task for this.
|
||||
*/
|
||||
public class UrlContentToString extends AsyncTask<URL, Integer, String> {
|
||||
|
||||
@Override
|
||||
protected String doInBackground(URL... urls) {
|
||||
String js = "";
|
||||
URL url = urls[0];
|
||||
try {
|
||||
js = new Scanner(url.openStream(), "UTF-8").useDelimiter("\\A")
|
||||
.next();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return js;
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user