2017-09-26 30 views
0

我在我的活动中使用Asynctask,并且我想使用asynctask获取url的html内容...我成功地在一段时间成功地将html内容成功了一段时间,但有时候并没有发生任何事情......我不能获得html的内容,并且它给了我黑色屏幕并给予ANR的错误...如何在某些应用程序活动中解决类似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这样的..

enter image description here

+0

它给我...输入调度超时错误... –

回答

0

应用程序未响应(ANR) - 如果我们在主(UI)线程中执行任何重要功能以及UI修改,将会发生。如果在UI线程中发生耗时的大量计算,它将延迟对用户操作的响应,这可能会刺激用户,从而停止进程。 Android 2.3.3以上的版本(姜饼)实际上严格地阻止了UI线程中的繁重处理,并允许用户使用ANR对话框关闭应用程序。

解决方案 - 只在主线程中运行UI组件,并将其他计算移动到后台线程。在这种情况下,将解析工作移至后台线程,并将解析结果传递给onPostExecute()调用。

注意 - 在任何情况下,您的应用程序执行可能冗长的操作,您不应该在UI线程上执行该工作,而是创建一个工作线程并完成大部分工作。

相关问题