create an async task for online js parsing

This commit is contained in:
Philipp Schaefer
2013-07-09 00:14:12 +02:00
parent c2634ce266
commit 7fcb1e0874
4 changed files with 60 additions and 15 deletions

View File

@ -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);
}