Android天气应用 - 无法加载图标

问题描述 投票:0回答:1

试图把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");

那不再适用了。如果有人可以帮助我,将不胜感激。谢谢

java android android-asynctask icons openweathermap
1个回答
0
投票

我的猜测是你没有正确地在调用方法上发送参数,这应该是这样的:

new DownloadImageAsyncTask().execute(new String [] { weather.iconData});

尝试调试或打印Utils.Icon_URL + code + ".png"的String结果,以检查它们是否与完整的URL匹配。

© www.soinside.com 2019 - 2024. All rights reserved.