Я пытаюсь создать приложение, которое обновит погоду нескольких городов. Каждая строка будет иметь разную временную температуру, поэтому я использую AsynkTask. Но я не могу обновить интерфейс после получения ответа от API.Asynk Задание вызова в цикле, не возвращающее значение
Мой Основная деятельность
public class MainActivity extends Activity {
LinearLayout depart_arrivals_details;
LayoutInflater inflater;
TextView depart_time,
depart_airport_city, depart_airport, arrival_time,
arrival_airport_city, arrival_airport, pnr_number,temprature,humidity;
ImageView flight_depart_image;
public static String url = "";
WeatherResponse response;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
depart_arrivals_details = (LinearLayout) findViewById(R.id.depart_arrivals_details);
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
setSectorData();
}
void setSectorData() {
for (int i = 0; i < 1; i++) {
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout layout = (RelativeLayout) inflater.inflate(
R.layout.sector_details, depart_arrivals_details, false);
depart_time = (TextView)layout.findViewById(R.id.depart_time);
depart_airport_city = (TextView)layout.findViewById(R.id.depart_airport_city);
temprature = (TextView)layout.findViewById(R.id.temprature);
humidity = (TextView)layout.findViewById(R.id.humidity);
flight_depart_image = (ImageView)layout.findViewById(R.id.flight_depart_image);
WeatherResponse responseUpdate = requestWeatherUpdate("DEL");
depart_time.setText("20:45");
depart_airport_city.setText("Mumbai");
/*
* This part will be updated when we will se the request and get the response
* then we have to set the temp and humidity for each city that we have recived
* */
temprature.setText(responseUpdate.getTempInC()+(char) 0x00B0);//Here it is showing null pointer exception after that the respone is coming from the server .So can we do this
humidity.setText(responseUpdate.getHumidity());
flight_depart_image.setImageResource(R.drawable.f1);
depart_arrivals_details.addView(layout, i);
}
}
/*
* Here the location will be dynamic and have to send the request for all the location i have
* */
private WeatherResponse requestWeatherUpdate(String location) {
url = "http://api.worldweatheronline.com/free/v1/weather.ashx?&format=xml&num_of_days=2&key=uysakmq923nbd5y549yz3aaw&q="
+ location;
Log.d("URL for Weather Upadate", url);
WeatherUpdateAsyncTask weatherReq = new WeatherUpdateAsyncTask(new CallBack() {
@Override
public void run(Object result) {
try {
String AppResponse = (String) result;
response = ParseWeatherResponseXML
.parseMyTripXML(AppResponse);
} catch (Exception e) {
Log.e("TAG Exception Occured",
"Exception is " + e.getMessage());
}
}
});
weatherReq.execute(url);
return response;
}
Это мой requestWeatherUpdate
private WeatherResponse requestWeatherUpdate(String location) {
url = ""
+ location;
Log.d("URL for Weather Upadate", url);
WeatherUpdateAsyncTask weatherReq = new WeatherUpdateAsyncTask(new CallBack() {
@Override
public void run(Object result) {
try {
String AppResponse = (String) result;
response = ParseWeatherResponseXML
.parseMyTripXML(AppResponse);
} catch (Exception e) {
Log.e("TAG Exception Occured",
"Exception is " + e.getMessage());
}
}
});
weatherReq.execute(url);
return response;
}
И если я пытаюсь установить значение, которое я полученый бросает Исключение нулевого указателя
использовать это для установки значений temprature.setText (parsedWeatherResponse.getTempInC() + (char) 0x00B0); – Developer