I'm trying to get table data from a certain website (this is the website) and I'm trying it out by trying to get a certain node. Here's my attempt:
public class ScheudeleWithDesign extends Activity {
static final String urlToParse = "https://www.easistent.com/urniki/263/razredi/18221";
static final String xpathTableContents = "//div[@id='text11']/td/tr";
TextView tw1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scheudele_with_design);
tw1 = (TextView) findViewById(R.id.urnikText);
String value = "";
try {
value = getScheudele();
tw1.setText(value);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XPatherException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}//End of onCreate
public String getScheudele() throws IOException, XPatherException{
String stats = null;
//cleaner properties
HtmlCleaner cleaner = new HtmlCleaner();
CleanerProperties props = cleaner.getProperties();
props.setAllowHtmlInsideAttributes(false);
props.setAllowMultiWordAttributes(false);
props.setRecognizeUnicodeChars(true);
props.setOmitComments(true);
//URL object
URL url = new URL(urlToParse);
//HTML page root node
TagNode root = cleaner.clean(url);
//query XPath
Object[] node = root.evaluateXPath(xpathTableContents);
//Vzemi podatke če najdeš element
if (node.length > 0) {
TagNode resultNode = (TagNode)node[0];
stats = resultNode.getText().toString();
}
return stats;
}
This doesn't work, the app crashes and this it the logcat:
09-22 11:22:08.632: E/AndroidRuntime(29385): FATAL EXCEPTION: main
09-22 11:22:08.632: E/AndroidRuntime(29385): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.whizzapps.stpsurniki/com.whizzapps.stpsurniki.ScheudeleWithDesign}: android.os.NetworkOnMainThreadException
09-22 11:22:08.632: E/AndroidRuntime(29385): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
09-22 11:22:08.632: E/AndroidRuntime(29385): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-22 11:22:08.632: E/AndroidRuntime(29385): at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-22 11:22:08.632: E/AndroidRuntime(29385): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-22 11:22:08.632: E/AndroidRuntime(29385): at android.os.Handler.dispatchMessage(Handler.java:99)
09-22 11:22:08.632: E/AndroidRuntime(29385): at android.os.Looper.loop(Looper.java:137)
09-22 11:22:08.632: E/AndroidRuntime(29385): at android.app.ActivityThread.main(ActivityThread.java:5103)
09-22 11:22:08.632: E/AndroidRuntime(29385): at java.lang.reflect.Method.invokeNative(Native Method)
09-22 11:22:08.632: E/AndroidRuntime(29385): at java.lang.reflect.Method.invoke(Method.java:525)
09-22 11:22:08.632: E/AndroidRuntime(29385): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-22 11:22:08.632: E/AndroidRuntime(29385): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-22 11:22:08.632: E/AndroidRuntime(29385): at dalvik.system.NativeStart.main(Native Method)
09-22 11:22:08.632: E/AndroidRuntime(29385): Caused by: android.os.NetworkOnMainThreadException
09-22 11:22:08.632: E/AndroidRuntime(29385): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
09-22 11:22:08.632: E/AndroidRuntime(29385): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
09-22 11:22:08.632: E/AndroidRuntime(29385): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
09-22 11:22:08.632: E/AndroidRuntime(29385): at java.net.InetAddress.getAllByName(InetAddress.java:214)
09-22 11:22:08.632: E/AndroidRuntime(29385): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
09-22 11:22:08.632: E/AndroidRuntime(29385): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
09-22 11:22:08.632: E/AndroidRuntime(29385): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
09-22 11:22:08.632: E/AndroidRuntime(29385): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
09-22 11:22:08.632: E/AndroidRuntime(29385): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
09-22 11:22:08.632: E/AndroidRuntime(29385): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
09-22 11:22:08.632: E/AndroidRuntime(29385): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:461)
09-22 11:22:08.632: E/AndroidRuntime(29385): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433)
09-22 11:22:08.632: E/AndroidRuntime(29385): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
09-22 11:22:08.632: E/AndroidRuntime(29385): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
09-22 11:22:08.632: E/AndroidRuntime(29385): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)
09-22 11:22:08.632: E/AndroidRuntime(29385): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
09-22 11:22:08.632: E/AndroidRuntime(29385): at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:271)
09-22 11:22:08.632: E/AndroidRuntime(29385): at java.net.URL.openStream(URL.java:462)
09-22 11:22:08.632: E/AndroidRuntime(29385): at org.htmlcleaner.Utils.readUrl(Utils.java:63)
09-22 11:22:08.632: E/AndroidRuntime(29385): at org.htmlcleaner.HtmlCleaner.clean(HtmlCleaner.java:373)
09-22 11:22:08.632: E/AndroidRuntime(29385): at org.htmlcleaner.HtmlCleaner.clean(HtmlCleaner.java:387)
09-22 11:22:08.632: E/AndroidRuntime(29385): at com.whizzapps.stpsurniki.ScheudeleWithDesign.getScheudele(ScheudeleWithDesign.java:63)
09-22 11:22:08.632: E/AndroidRuntime(29385): at com.whizzapps.stpsurniki.ScheudeleWithDesign.onCreate(ScheudeleWithDesign.java:36)
09-22 11:22:08.632: E/AndroidRuntime(29385): at android.app.Activity.performCreate(Activity.java:5133)
09-22 11:22:08.632: E/AndroidRuntime(29385): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-22 11:22:08.632: E/AndroidRuntime(29385): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
09-22 11:22:08.632: E/AndroidRuntime(29385): ... 11 more
The exception that is thrown when an application attempts to perform a networking operation on its main thread.
This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it's heavily discouraged
Source: http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html
Solution #1:
Execute the network call in a separeate thread, try using AsyncTask.
Solution #2:
You can use StrictMode object to allow 'network on main thread'.
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
P.S.: Use it only if you fully understands the side effects of this solution. You can find more information about StrictMode here.