Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 执行doInBackground()时发生异步错误_Android_Asynchronous - Fatal编程技术网

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

Android 执行doInBackground()时发生异步错误,android,asynchronous,Android,Asynchronous,Stacktrace: try{ String data = ((new WeatherHttpClient()).getWeatherData(params[0])); weather = JSONWeatherParser.getWeather(data); }catch(RuntimeException e){ Log.d('',e.getMessage()); } return weather; } 需要帮忙吗?我自己找不到正确的解决方案。如果你想运行长时间操作,我建议你在do

Stacktrace:

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

weather = JSONWeatherParser.getWeather(data);

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


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

如果你想运行长时间操作,我建议你在doInBackground内部使用try catch with RuntimeException,我在使用jsoap抓取web内容时也会遇到这个问题。当我将try-catch与RuntimeException一起使用时,错误消失了

编辑:

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

weather = JSONWeatherParser.getWeather(data);

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

顺便说一句,我想你也需要发布你的JSONWeatherParser类,因为我需要知道NullPointerException发生在哪里

你在
getWeatherData
上收到一个
IOException
,返回
null
并执行
JSONWeatherParser.getWeather(数据)data
null
时,code>是,但为什么数据为null?
data
null
,因为您在
getWeatherData
上接收到
IOException
并返回
null
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;
}
try{
String data = ((new WeatherHttpClient()).getWeatherData(params[0]));

weather = JSONWeatherParser.getWeather(data);

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