WeatherdemoNew

原创
2015/05/22 16:33
阅读数 49

Json:

package com.weatherdemo.until;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;


public class JsonActivityNew {
 public static Map<String, String> getJsonLogin(String str)
   throws JSONException {
  JSONObject obj = new JSONObject(str);
  String resultcode = obj.getString("resultcode");
  String reason = obj.getString("reason");

  Map<String, String> map = new HashMap<String, String>();
  map.put("resultcode", resultcode);
  map.put("reason", reason);
  return map;
 }

 /**
  * 解析表单
  *
  */
 public static List<Weather> getMenuDemoh(String str) throws JSONException {
  List<Weather> arr = new ArrayList<Weather>();
  List<Map<String,String>> list=new ArrayList<Map<String,String>>();
  
  Map<String,String> map=null;
  Weather wDemo = null;
  Demo demo=null;
  JSONObject obj = new JSONObject(str);
  String reason = obj.getString("reason");
  String resultcode = obj.getString("resultcode");
  int error_code = obj.getInt("error_code");


  JSONArray ja = obj.getJSONArray("result");
  for (int i = 0; i < ja.length(); i++) {
   JSONObject jb = ja.getJSONObject(i);
   
   String weatherid = jb.getString("weatherid");
   String weather = jb.getString("weather");
   String temp1 = jb.getString("temp1");
   String temp2 = jb.getString("temp2");
   String sh = jb.getString("sh");
   String eh = jb.getString("eh");
   String date = jb.getString("date");
   String sfdate = jb.getString("sfdate");
   String efdate = jb.getString("efdate");
   
   map=new HashMap<String, String>();
   map.put("weatherid",weatherid );
   map.put("weather",weather );
   map.put("temp1",temp1 );
   map.put("temp2",temp2 );
   map.put("sh",sh );
   map.put("eh",eh );
   map.put("date",date );
   map.put("sfdate",sfdate );
   map.put("efdate",efdate );
   
   list.add(map);
   
////   demo=new Demo(weatherid, weather, temp1, temp2, sh, eh, date, sfdate, efdate);
//   demo=new Demo();
//   demo.setWeatherid(weatherid);
//   demo.setDate(efdate);
//   demo.setEfdate(efdate);
//   demo.setEh(eh);
//   demo.setSfdate(sfdate);
//   demo.setSh(sh);
//   demo.setTemp1(temp1);
//   demo.setTemp2(temp2);
//   demo.setWeather(weather);
   
   
  }
//  System.out.println("list-----"+list);
  System.out.println("示范-----"+list.get(0).get("weather"));
  wDemo=new Weather(resultcode, reason, demo, error_code);
  
  arr.add(wDemo);
  
//  System.out.println("arr---"+arr);
  return arr;
 }
}

 

package com.weatherdemo;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import com.weatherdemo.until.HttpUtil;
import com.weatherdemo.until.JsonActivityNew;
import com.weatherdemo.until.Weather;

public class WeatherDemoLogic {
 /**
  * 获取网上菜单表内容
  */
 public List<Weather> WeatherDemo(String cityname) throws ClientProtocolException, IOException,
   JSONException {
 
  Map<String, String> map = new HashMap<String, String>();
  map.put("key", "c1bb4fcbf131db6aa9b451d52b09cbae");
  map.put("dtype", "json");
  map.put("cityname", cityname);
  
  String str = HttpUtil.getRequest("forecast3h", map);
  
//System.out.println("str---"+str);
  
  List<Weather> requst = JsonActivityNew.getMenuDemoh(str);
  
  return requst;
}
}

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部