繁体   English   中英

Android Weather App-无法加载图标

[英]Android weather app - can't load the Icon

尝试将图标放到我的android weather app中。

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");

那不再起作用了。 如果有人可以帮助我,那将是很棒的。 谢谢

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM