-2

我正在尝试获取以下 JSON 中显示的第一张图片:

{
    "hourly_forecast": [{
            "FCTTIME": {
                "hour": "0",
                "hour_padded": "00",
                "min": "00",
                "sec": "0",
                "year": "2013",
                "mon": "10",
                "mon_padded": "10",
                "mon_abbrev": "Oct",
                "mday": "30",
                "mday_padded": "30",
                "yday": "302",
                "isdst": "0",
                "epoch": "1383087600",
                "pretty": "12:00 AM CET on October 30, 2013",
                "civil": "12:00 AM",
                "month_name": "October",
                "month_name_abbrev": "Oct",
                "weekday_name": "Wednesday",
                "weekday_name_night": "Wednesday Night",
                "weekday_name_abbrev": "Wed",
                "weekday_name_unlang": "Wednesday",
                "weekday_name_night_unlang": "Wednesday Night",
                "ampm": "AM",
                "tz": "",
                "age": ""
            },

            "temp": {
                "english": "47",
                "metric": "8"
            },
            "dewpoint": {
                "english": "44",
                "metric": "6"
            },
            "condition": "Mostly Cloudy",
            "icon": "mostlycloudy",
            "icon_url": "http://icons-ak.wxug.com/i/c/k/nt_mostlycloudy.gif",

            "fctcode": "3",
            "sky": "71",
            "wspd": {
                "english": "11",
                "metric": "18"
            },
            "wdir": {
                "dir": "SW",
                "degrees": "231"
            },
            "wx": "",
            "uvi": "0",
            "humidity": "86",
            "windchill": {
                "english": "-9998",
                "metric": "-9998"
            },
            "heatindex": {
                "english": "-9998",
                "metric": "-9998"
            },
            "feelslike": {
                "english": "47",
                "metric": "8"
            },
            "qpf": {
                "english": "",
                "metric": ""
            },
            "snow": {
                "english": "",
                "metric": ""
            },
            "pop": "10",
            "mslp": {
                "english": "29.93",
                "metric": "1013"
            }
        },

每小时重复一次,但我只需要第一张图像。如何制作一个AyncTask只返回标签“icon_url”找到的第一张图片?

我到目前为止的代码:

protected Void doInBackground(String... params) {
    String response = null;
    String url = "http://api.wunderground.com/api/apikey/hourly/q/CA/"
            + toStation + ".json";
    Time now = new Time();
    now.setToNow();
    try {
        HttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);

        ResponseHandler<String> handler = new BasicResponseHandler();
        response = client.execute(httpGet, handler);
        JSONArray list = new JSONArray(response);

        for (int i = 0; i < list.length(); i++) {
            JSONObject wheatherObj = list.getJSONObject(i);
            Wheather wheather = new Wheather(wheatherObj);
            if(wheather.getTime()==now.hour+"") {
                <what needs to be here>
            }
        }
4

1 回答 1

0

这个:

URL bmpUrl = new URL(wheatherObj.getString("icon_url"));
final Bitmap bmp = BitmapFactory.decodeStream(bmpUrl.openConnection().getInputStream());

应该给你你的位图。要将其设置为 ImageView,您可以在以下操作后执行此操作:

final ImageView imageView=(ImageView)findViewById(R.id.imageView);
MainActivity.this.runOnUiThread(new Runnable()
{
    @Override
    public void run()
    {
      imageView.setImageBitmap(bmp);
    }
}

不要忘记在尝试/最终关闭您的连接。

于 2013-10-29T22:57:10.840 回答