我在我的活动中使用Asynctask,我想使用asynctask获取url的html内容...我在一段时间内成功地修改了html内容但有时候没有发生任何事情......我无法获得html及其内容给我黑屏并给出ANR的错误...
我的活动编码......
public class ControlActivity extends AppCompatActivity {
private static TextView txt1, txt2, txt3, txt4,txt5,txt6,txt7,txt8;
private static SwitchCompat switch1, switch2, switch3, switch4, switch5, switch6, switch7, switch8;
// private static ImageView image1,image2,image3,image4,image5,image6,image7,image8;
private static Button allon,alloff;
String t1,t2,t3,t4,t5,t6,t7,t8;
SwipeRefreshLayout swipe_container;
JSONArray data = null;
Handler mHandler;
private static String ip,port,uname,password;
private static Document htmlDocument;
private static String htmlContentInStringFormat,content;
private static String stringuri;
private static List<String> listOfString = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_control);
new JSONAsyncTask().execute();
class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Boolean doInBackground(String... urls) {
StringBuffer stringBuffer = new StringBuffer("");
BufferedReader bufferedReader = null;
try {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet();
URI uri = new URI("http://"+ip+":"+port+"/index.htm");
httpGet.setURI(uri);
httpGet.addHeader(BasicScheme.authenticate(
new UsernamePasswordCredentials(uname, password),
HTTP.UTF_8, false));
HttpResponse httpResponse = httpClient.execute(httpGet);
// Read the contents of an entity and return it as a String.
content = EntityUtils.toString(httpResponse.getEntity());
Log.e("content: ", "> " + content);
// listOfString.clear();
InputStream inputStream = httpResponse.getEntity().getContent();
bufferedReader = new BufferedReader(new InputStreamReader(
inputStream));
String readLine = bufferedReader.readLine();
while (readLine != null) {
stringBuffer.append(readLine);
stringBuffer.append("\n");
readLine = bufferedReader.readLine();
}
} catch (Exception e) {
// TODO: handle exception
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
// TODO: handle exception
}
}
}
return false;
}
protected void onPostExecute(Boolean result) {
txt1=(TextView)findViewById(R.id.txt1);
txt2=(TextView)findViewById(R.id.txt2);
txt3=(TextView)findViewById(R.id.txt3);
txt4=(TextView)findViewById(R.id.txt4);
txt5=(TextView)findViewById(R.id.txt5);
txt6=(TextView)findViewById(R.id.txt6);
txt7=(TextView)findViewById(R.id.txt7);
txt8=(TextView)findViewById(R.id.txt8);
Document doc = Jsoup.parse(content);
htmlContentInStringFormat = doc.title();
Elements td=doc.getElementsByTag("td");
//Log.e("td: ", "> " + td);
String td1=td.toString();
// Log.e("td1: ", "> " + td1);
Elements articles = doc.select("td");
for (Element element : articles) {
String content1 = element.text();
Log.e("content1: ", "> " + content1);
listOfString.add(content1);
System.out.println(content1);
}
t1 = listOfString.get(26);
t2 = listOfString.get(31);
t3 = listOfString.get(36);
t4 = listOfString.get(41);
t5 = listOfString.get(46);
t6 = listOfString.get(51);
t7 = listOfString.get(56);
t8 = listOfString.get(61);
Log.e("t1: ", "> " + t1);
Log.e("t2: ", "> " + t2);
Log.e("t3: ", "> " + t3);
Log.e("t4: ", "> " + t4);
txt1.setText(t1);
txt2.setText(t2);
txt3.setText(t3);
txt4.setText(t4);
txt5.setText(t5);
txt6.setText(t6);
txt7.setText(t7);
txt8.setText(t8);
}
}
}
它给我这样的roor ..
答案 0 :(得分:0)
应用程序无响应(ANR) - 如果我们在主(UI)线程中执行任何繁重的功能以及UI修改,将会发生。如果在UI线程中发生耗时的繁重计算,则会延迟对用户操作的响应,这可能会刺激用户,从而停止进程。事实上,2.3.3以上的Android版本(Gingerbread)严格禁止在UI线程中进行大量处理,并允许用户使用ANR对话框关闭应用程序。
解决方案 - 仅在主线程中运行UI组件,并将其他计算移至后台线程。在这种情况下,将解析工作移到后台线程,并将解析后的结果传递给onPostExecute()调用。
注意 - 在您的应用执行可能冗长的操作的任何情况下,您都不应该在UI线程上执行工作,而是创建工作线程并在那里完成大部分工作。