Java Android htmlcleaning
我使用此代码读取html页面,但我有一个错误…我如何解决这个问题 错误:Java Android htmlcleaning,java,android,Java,Android,我使用此代码读取html页面,但我有一个错误…我如何解决这个问题 错误: FATAL EXCEPTION: main Process: com.example.nuovo, PID: 1615 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nuovo/com.example.nuovo.Nuovo_Activity}: android.os.NetworkOnMainThreadEx
FATAL EXCEPTION: main
Process: com.example.nuovo, PID: 1615
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nuovo/com.example.nuovo.Nuovo_Activity}: android.os.NetworkOnMainThreadException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
代码:
package com.example.nuovo;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;
import android.util.Log;
public class test {
public String getRis() throws MalformedURLException, IOException, XPatherException{
String result="aa";
Log.d("prova di funzionamento","test");
CleanerProperties props = new CleanerProperties();
// set some properties to non-default values
props.setTranslateSpecialEntities(true);
props.setTransResCharsToNCR(true);
props.setOmitComments(true);
URL url=new URL("https...........aspx");
Log.d("prova di funzionamentoPost1","test");
// do parsing
TagNode tagNode = new HtmlCleaner(props).clean(url);
Log.d("prova di funzionamentoPost2","test");
Object[] nodes = tagNode.evaluateXPath("//table[@class='TableQuotazioni']");
TagNode node = (TagNode)nodes[0];
nodes = node.evaluateXPath("//td");
Log.d("prova di funzionamento2","test");
for (int i = 1; i < nodes.length; i++) {
String ris= (((TagNode)nodes[i]).getText()).toString();
result=ris;
}
return result;
}
}
在Android中,您不能在主线程中使用internet,这就是为什么会出现NetworkOnMainThreadException。使用AsyncTask以使用网络。下面是AsyncTask类的一个简单示例
class MyAsyncTask extends AsyncTask<Void, Void, Void> {
myAsyncTask() {
}
// Executed on the UI thread before the
// time taking task begins
@Override
protected void onPreExecute() {
super.onPreExecute();
}
// Executed on a special thread and all your
// time taking tasks should be inside this method
@Override
protected Void doInBackground(Void... arg0) {
// Put your work with network here
return null;
}
// Executed on the UI thread after the
// time taking process is completed
@Override
protected void onPostExecute(Void result) {
//Update your views here
super.onPostExecute(result);
}
} }
添加PermitNetworks.Class
import android.os.StrictMode;
public class PermitNetworks {
public static final void networkAllPermit() {
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new
StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
}
}
然后添加以上所有表示http连接的代码
PermitNetworks.networkAllPermit();
我希望你能从中得到解决
PermitNetworks.networkAllPermit();