试图把Icon放到我的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");
这不再适用了。如果有人可以帮助我,那将是非常好的。谢谢
答案 0 :(得分:0)
我的猜测是你没有在调用方法上正确发送参数,这应该是这样的:
new DownloadImageAsyncTask().execute(new String [] { weather.iconData});
尝试调试或打印Utils.Icon_URL + code + ".png"
的字符串结果,以检查它们是否与完整的URL匹配。