在我的Android代码中,我有AsyncTask
,似乎无法转换为简单的Public void
函数。
private class JSONWeatherTask extends AsyncTask<String, Void, Weather> {
@Override
protected Weather doInBackground(String... params) {
Weather weather = new Weather();
String data = ((new WeatherHttpClient()).getWeatherData(params[0]));
// WeatherC = params[0];
try {
weather = JSONWeatherParser.getWeather(data);
// Let's retrieve the icon
weather.iconData = ((new WeatherHttpClient()).getImage(weather.currentCondition.getIcon()));
} catch (JSONException e) {
e.printStackTrace();
}
return weather;
}
@Override
protected void onPostExecute(Weather weather) {
super.onPostExecute(weather);
if (weather.iconData != null && weather.iconData.length > 0) {
// Bitmap img = BitmapFactory.decodeByteArray(weather.iconData, 0, weather.iconData.length);
//imgView.setImageBitmap(img);
}
WeatcherCels = ("" + Math.round((weather.temperature.getTemp() - 273.15)) + " C");
Toast.makeText(context, WeatcherCels, Toast.LENGTH_SHORT).show();
}
}
我执行task
的方式是:
JSONWeatherTask task = new JSONWeatherTask();
task.execute(new String[]{"canada,toronto"});
但是,我试图使用以下内容:
String WeatherStr = getWeather("canada,toronto");
答案 0 :(得分:0)
你可以这样做:
创建这样的方法:
String getWeather(String... arr)
{
JSONWeatherTask task = new JSONWeatherTask();
task.execute(arr);
return task.get();
}
然后
String weather = getWeather("a","b");
但它会阻止ui,直到asyntask doinbackground完成。