错误
FATAL EXCEPTION: AsyncTask #1
Process: com.example.brucewayne.weather, PID: 28994
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 com.example.brucewayne.weather.MainActivity$WeatherTask.doInBackground(MainActivity.java:74)
at com.example.brucewayne.weather.MainActivity$WeatherTask.doInBackground(MainActivity.java:69)
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)
我的JSONWeatherParser类
public class JSONWeatherParser {
public static Weather getWeather(String data){
Weather weather = new Weather();
//create Json object from data
try {
JSONObject jsonObject = new JSONObject(data);
Place place = new Place();
JSONObject coordObj = Utils.getObject("coord",jsonObject);
place.setLat(Utils.getFloat("lat",coordObj));
place.setLon(Utils.getFloat("lon",coordObj));
//Get Sys Object in JSON web file http://samples.openweathermap.org/data/2.5/weather?q=London&appid=b1b15e88fa797225412429c1c50c122a1
JSONObject sysObj= Utils.getObject("sys",jsonObject);
place.setCountry(Utils.getString("country",sysObj));
//dt is outside the sub branch in JSON so jsonObject used
place.setLastupdate(Utils.getInt("dt",jsonObject));
place.setSunrise(Utils.getInt("sunrise",sysObj));
place.getSunset(Utils.getInt("sunset",sysObj));
place.setCity(Utils.getString("name",jsonObject));
weather.place = place;
//get weather(its an array in jason file) info
JSONArray jsonArray = jsonObject.getJSONArray("weather");
//"weather":[{"id":300(//0 is the index//),"main":"Drizzle","description":"light intensity drizzle","icon":"09d"}],
JSONObject jsonWeather = jsonArray.getJSONObject(0);
weather.currentCondition.setWeatherId(Utils.getInt("id",jsonWeather));
weather.currentCondition.setDescription(Utils.getString("description",jsonWeather));
weather.currentCondition.setCondition(Utils.getString("main",jsonWeather));
weather.currentCondition.setIcon(Utils.getString("icon",jsonWeather));
JSONObject windObj = Utils.getObject("wind",jsonObject);
weather.wind.setSpeed(Utils.getFloat("speed",windObj));
weather.wind.setDeg(Utils.getFloat("deg",windObj));
JSONObject cloudObj = Utils.getObject("clouds",jsonObject);
weather.clouds.setPrecipitation(Utils.getInt("all",cloudObj));
return weather;
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
我的回复者和异步
public void renderWeatherData(String city){
WeatherTask weatherTask = new WeatherTask();
weatherTask.execute(new String[]{city + "&units=metric"});
}
private class WeatherTask extends AsyncTask<String,Void, Weather>{
@Override
protected Weather doInBackground(String... strings) {
String data = ((new WeatherHttpClient()).getWeatherData(strings[0]));
weather = JSONWeatherParser.getWeather(data);
Log.v("Data:",weather.currentCondition.getDescription());
return weather;
}
@Override
protected void onPostExecute(Weather weather) {
super.onPostExecute(weather);
}
}
答案 0 :(得分:0)
为什么要将String数组传递给execute方法?
weatherTask.execute(new String[]{city + "&units=metric"});
AsyncTask的execute方法具有此声明execute(Params... params)
查看文档:{{3}}
所以,在你的情况下,你应该这样称呼它:
weatherTask.execute(city + "&units=metric");