我正在尝试获取以下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>
}
}
答案 0 :(得分: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);
}
}
不要忘记在try / finally中关闭你的连接。