【问题标题】:Android weather app - can't load the IconAndroid 天气应用程序 - 无法加载图标
【发布时间】: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


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-12
      • 2017-07-22
      • 1970-01-01
      • 1970-01-01
      • 2018-09-17
      相关资源
      最近更新 更多