簡體   English   中英

如何在應用程序的某些活動中解決諸如ANR之類的錯誤?

[英]How to resolve error like ANR in some activity of application?

我在活動中使用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);

        }
    }
}

這樣給我帶來了回報。

在此處輸入圖片說明

應用程序無響應(ANR) -如果我們在Main(UI)線程中做任何繁重的功能以及UI修改,就會發生。 如果UI線程中進行了耗時的大量計算,它將延遲對用戶操作的響應,這可能會激怒用戶,從而停止您的進程。 實際上,高於2.3.3(Gingerbread)的Android版本嚴格不鼓勵在UI線程中進行繁重的處理,並允許用戶使用ANR對話框關閉應用程序。

解決方案 -僅在主線程中運行UI組件,並將其他計算移至后台線程。 在這種情況下,將解析工作移至后台線程,並將解析的結果傳遞給onPostExecute()調用。

注意 -在任何情況下,如果您的應用程序可能執行冗長的操作,則不應在UI線程上執行工作,而應創建工作線程並在其中執行大部分工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM