返回值有时在 AsyncTask 中为 null



我通过调用一个简单的选择查询来接收 JSON 格式的值。我正在使用AsyncTask。问题是有时它工作正常,但有时response_value为空,没有任何变化。我该怎么办?我错过了什么?

doInBackground(String... strings)代码

URL url = new URL(GetUrl.URL_MAIN_CATEGORIES_DATA);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoInput(true);
                httpURLConnection.setDoOutput(true);
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
                String response_value = "";
                String line = "";
                while ((line = bufferedReader.readLine()) != null) {
                    response_value += line;
                }
                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
                return response_value;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (ProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

尝试在 catch 块中记录异常,而不是打印堆栈跟踪。

Log.e("Yourtag","Exception occurred",e);

您必须收到异常,但您无法通过 android 上的堆栈跟踪看到它。这是它有时可以工作而有时不能工作的唯一方法。

最新更新