执行doInBackground()时发生异步错误

时间:2016-06-26 15:42:26

标签: android asynchronous

堆栈跟踪:

06-26 15:30:45.019 2960-2996/yonitheweatherapp.theweatherapp E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
                                                                           Process: yonitheweatherapp.theweatherapp, PID: 2960
                                                                           java.lang.RuntimeException: An error occurred while executing doInBackground()
                                                                               at android.os.AsyncTask$3.done(AsyncTask.java:309)
                                                                               at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                                                                               at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                                                                               at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                                               at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
                                                                               at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                                                                               at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                                               at java.lang.Thread.run(Thread.java:818)
                                                                            Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
                                                                               at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
                                                                               at org.json.JSONTokener.nextValue(JSONTokener.java:94)
                                                                               at org.json.JSONObject.<init>(JSONObject.java:156)
                                                                               at org.json.JSONObject.<init>(JSONObject.java:173)
                                                                               at data.JSONWeatherParser.getWeather(JSONWeatherParser.java:22)
                                                                               at yonitheweatherapp.theweatherapp.MainActivity$WeatherTask.doInBackground(MainActivity.java:66)
                                                                               at yonitheweatherapp.theweatherapp.MainActivity$WeatherTask.doInBackground(MainActivity.java:60)
                                                                               at android.os.AsyncTask$2.call(AsyncTask.java:295)
                                                                               at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                               at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
                                                                               at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
                                                                               at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
                                                                               at java.lang.Thread.run(Thread.java:818) 

06-26 15:30:45.780 2960-2999 / yonitheweatherapp.theweatherapp E / Surface:getSlotFromBufferLocked:unknown buffer:0xae4513f0

doInBackground():

    private class WeatherTask extends AsyncTask<String, Void, Weather>{
    @Override
    protected Weather doInBackground(String... params) {

        String data = ((new WeatherHttpClient()).getWeatherData(params[0]));

        weather = JSONWeatherParser.getWeather(data);

        return weather;
    }

    @Override
    protected void onPostExecute(Weather weather) {
        super.onPostExecute(weather);
    }

WeatherHttpClient(): 公共类WeatherHttpClient {

public String getWeatherData(String place){
    HttpURLConnection connection = null;
    InputStream inputStream = null;

    try {
        connection = (HttpURLConnection) (new URL(Utils.BASE_URL + place)).openConnection();
        connection.setRequestMethod("GET");
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.connect();

        //read the response
        StringBuffer stringBuffer = new StringBuffer();
        inputStream = connection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

        String line = null;

        while((line = bufferedReader.readLine()) != null){
            stringBuffer.append(line + "\r\n");
        }



        inputStream.close();
        connection.disconnect();

        return stringBuffer.toString();

    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

}

请帮忙吗?我自己找不到正确的解决方案。

1 个答案:

答案 0 :(得分:1)

如果你想运行长时间操作,我建议你在doInBackground中使用带有RuntimeException的try catch,我在使用jsoap抓取web内容时也遇到了这个问题。当我使用带有RuntimeException的try catch时错误消失了。

修改

try{
String data = ((new WeatherHttpClient()).getWeatherData(params[0]));

weather = JSONWeatherParser.getWeather(data);

}catch(RuntimeException e){
  Log.d('',e.getMessage());
}
return weather;

顺便说一下,我认为您还需要发布您的JSONWeatherParser类,因为我需要知道NullPointerException发生的位置