首页 > 解决方案 > 如何调试 AsyncTask.doInBackground?

问题描述

我想在 android studio 中使用异步任务在与我相同的网络上执行 php 脚本并获得结果。

private class HTTPReqTask extends AsyncTask<String, String, String> {

        String temp = new String();
        @Override
        protected String doInBackground(String... strings) {
            HttpURLConnection urlConnection = null;

            try {
                URL url = new URL("PHPSCRIBT" + strings[0]);
                HttpURLConnection con = (HttpURLConnection) url.openConnection();
                con.setRequestMethod("GET");
                int status = con.getResponseCode();

                StringBuilder sb = new StringBuilder();

                String line;
                BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
                temp = sb.toString();
                Log.d("myTag", temp);
                con.disconnect();
            } catch (Exception e) {

            }
            return temp;
        }


        protected void onPostExecute(String temp) {
            TextView textView = (TextView) findViewById(R.id.textView5);
            try {
                String erstezahl = temp.split("\\{")[0];
                String jsonstring = temp.substring(temp.indexOf("{") - 1);

                JSONObject json = new JSONObject(jsonstring);

                int amount = Integer.valueOf(erstezahl);
                textView.setText(amount);
            } catch (Exception e) {
                String error = "ERROR +" + temp + "+" + e.toString();
                textView.setText(error);
            }


        }
    }

我的 onPostExecute() 函数获取一个空字符串作为参数。因此,我想调试我的 doInBackground 函数。做这个的最好方式是什么?

标签: androidasynchronous

解决方案


推荐阅读