我创建了这个应用程序,以便为我的应用程序提供更详细的信息。
我的Android应用程序中有一个GetWeather类,我有GetWeather方法,它返回一个字符串。但是当我试图获得由一个线程组成的类的值时。我总是得到一个空值。请亲切地参考我的代码并告诉我哪里出错了。谢谢
主要活动
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = (Button) findViewById(R.id.showData);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
GetWeather1 gw = new GetWeather1();
String weather = gw.GetWeather1 ("CA","Anuradhapura");
Toast.makeText(getApplicationContext(), " Weather condition is " + weather, Toast.LENGTH_SHORT).show();
}
});
}
// This is GetWeather class
public class GetWeather {
public String weather;
public String temperature_string;
public Bitmap weather_icon;
public GetWeather() {
}
public String GetWeather(String city, String state) {
city = city.replaceAll(" ", "_");
// construct post URL
final String GET_WEATHER_URL = WEATHER_URL + state + "/" + city
+ ".json";
new Thread(new Runnable() {
public void run() {
String request = GET_WEATHER_URL;
HttpResponse rp = null;
JSONObject jObject = null;
try {
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(
CoreProtocolPNames.PROTOCOL_VERSION,
HttpVersion.HTTP_1_1);
HttpGet request1 = new HttpGet(
"http://api.wunderground.com/api/key/conditions/q/CA/Anuradhapura.json");
HttpResponse response = httpclient.execute(request1);
HttpEntity resEntity = response.getEntity();
String _response = EntityUtils.toString(resEntity);
jObject = new JSONObject(_response);
JSONObject current_observation = jObject.getJSONObject("current_observation");
temperature_string = current_observation.getString("temperature_string");
weather = current_observation.getString("weather");
Log.i("..............", "" + weather);
Log.i("..............", "" + temperature_string);
String icon_url = current_observation.getString("icon_url");
weather_icon = get_weather_icon(icon_url);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
while (weather != null) {
}
return weather;
}
答案 0 :(得分:3)
条件while(whether != null)
应while(whether == null)
等待Thread
完成其任务。但它会在UI Thread上等待,这可能会导致ANR
错误。
而是使用AsyncTask
并在onPostExecute()
...
关于您的意见,这可能会对您有所帮助。
public class WeatherTask extends AsyncTask<String, Void, String[]> {
@Override
protected String[] doInBackground(String... params) {
String city = params[0];
String state = params[1];
city = city.replaceAll(" ", "_");
// construct post URL
final String GET_WEATHER_URL = WEATHER_URL + state + "/" + city
+ ".json";
String request = GET_WEATHER_URL;
HttpResponse rp = null;
JSONObject jObject = null;
try {
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(
CoreProtocolPNames.PROTOCOL_VERSION,
HttpVersion.HTTP_1_1);
HttpGet request1 = new HttpGet(
"http://api.wunderground.com/api/key/conditions/q/CA/Anuradhapura.json");
HttpResponse response = httpclient.execute(request1);
HttpEntity resEntity = response.getEntity();
String _response = EntityUtils.toString(resEntity);
jObject = new JSONObject(_response);
JSONObject current_observation = jObject
.getJSONObject("current_observation");
String temperature_string = current_observation
.getString("temperature_string");
String weather = current_observation.getString("weather");
Log.i("..............", "" + weather);
Log.i("..............", "" + temperature_string);
String icon_url = current_observation.getString("icon_url");
String weather_icon = get_weather_icon(icon_url);
String[] out = new String[]{weather,weather_icon,temperature_string};
return out;
} catch (Exception exception) {
}
return null;
}
@Override
protected void onPostExecute(String[] result) {
if(result != null) {
String weather = result[0];
String weather_icon = result[1];
String temperature_string = result[2];
}
}
}
并在onButtonClick()
中开始执行此任务
new WeatherTask().execute("CA","Anuradhapura");