【发布时间】:2017-12-12 00:51:31
【问题描述】:
试图将图标放到我的 android 天气应用程序中。
private class DownloadImageAsyncTask extends AsyncTask<String, Void, Bitmap>{
@Override
protected Bitmap doInBackground(String... params) {
return downloadImage(params[0]);
}
@Override
protected void onPostExecute(Bitmap bitmap) {
iconView.setImageBitmap(bitmap);
}
private Bitmap downloadImage(String code){
final DefaultHttpClient client = new DefaultHttpClient();
final HttpGet getRequest =new HttpGet(Utils.Icon_URL + code + ".png");
//final HttpGet getRequest = new HttpGet("http://api.openweathermap.org/img/w/13n.png");
try {
HttpResponse response = client.execute(getRequest);
final int statusCode = response.getStatusLine().getStatusCode();
if(statusCode != HttpStatus.SC_OK){
Log.e("DownloadImage", "Error:" + statusCode);
return null;
}
final HttpEntity entity = response.getEntity();
if(entity != null){
InputStream inputStream = null;
inputStream = entity.getContent();
final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
private class WeatherTask extends AsyncTask<String, Void, Weather>{
//doInBacckgroudissa oleva koodit ajetaan taustalla. Eikä vaikuttaa muiden saikeiden ajaamista.
@Override
protected Weather doInBackground(String... params) {
String data = ((new WeatherHttpClient())).getWeatherData(params[0]);
weather.iconData = weather.currentCond.getIcon();
weather = JsonWeatherParser.getWeather(data);
new DownloadImageAsyncTask().execute(weather.iconData);
return weather;
}
我不知道,我该如何解决这个问题。它可以工作,当我像这样输入直接 URI 时
final HttpGet getRequest = new HttpGet("http://api.openweathermap.org/img/w/13n.png");
但如果我将其更改为:
public static final String Icon_URL = "http://api.openweathermap.org/img/w/";
final HttpGet getRequest =new HttpGet(Utils.Icon_URL + code + ".png");
这不再起作用了。如果有人可以帮助我,那就太好了。谢谢
【问题讨论】:
标签: java android android-asynctask icons openweathermap