Android Json解析
1、
public static final String ROOT_URL="http://xxx.xxx.xxx.xxx:xxxx/phone"; public static final String ACTION_GET_STORE_RECOMMEND="/store/getlist.aspx";//获得推荐商家
2、使用FinalHttp 形式很简单,但只能传键值对格式的参数给服务器
public static void getStoreRecommend() { FinalHttp fh = new FinalHttp(); String url = ROOT_URL + ACTION_GET_STORE_RECOMMEND; AjaxParams params = new AjaxParams(); params.put("page", "1");
params..put("cityid", "35"); fh.post(url,params, new AjaxCallBack<Object>(){ @Override public void onLoading(long count, long current) { // TODO Auto-generated method stub super.onLoading(count, current); } @Override public void onSuccess(Object t) { // TODO Auto-generated method stub super.onSuccess(t); Log.e("getStoreRecommend返回结果",t.toString()); //将json串转化为对象list List<StoreRecommend> list = JsonParser.ParserStoreRecommed(t.toString()); } }); }
3、如果传Json串给服务器 需要另外一种方法
public static void getStoreRecommend() { try { //创建连接 URL url = new URL(ROOT_URL + ACTION_GET_STORE_RECOMMEND); HttpURLConnection connection = (HttpURLConnection) url .openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("POST"); connection.setUseCaches(false); connection.setInstanceFollowRedirects(true); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.connect(); //POST请求 DataOutputStream out = new DataOutputStream( connection.getOutputStream()); JSONObject jsonobg = new JSONObject(); jsonobg.put("page", "1");//输入查看的页数 jsonobg.put("cityid", "35");//输入当前城市id //转化成json串 out.writeBytes(jsonobg.toString()); out.flush(); out.close(); //读取响应 BufferedReader reader = new BufferedReader(new InputStreamReader( connection.getInputStream())); String lines; StringBuffer sb = new StringBuffer(""); while ((lines = reader.readLine()) != null) { lines = new String(lines.getBytes(), "utf-8"); sb.append(lines); } System.out.println(sb); reader.close(); // 断开连接 connection.disconnect(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
4、将传回的json串 转化成对象
public static List<StoreRecommend> ParserStoreRecommed(String str) { try { JSONObject jobg = new JSONObject(str); String msg = jobg.getString(code); if ("-99".equals(msg)) { errorDetail = jobg.getString(error); }else if("-1".equals(msg)){ //参数错误 }else if("-2".equals(msg)){ //为空 }else if ("0".equals(msg)) { String result = jobg.getString("Stores"); Gson gson = new Gson(); //列表型 List<StoreRecommend> ps = gson.fromJson(result, new TypeToken<List<StoreRecommend>>() { }.getType()); //单个对象 //StoreRecommend ps = gson.fromJson(result, new StoreRecommend() { //}.getType());
return ps; } } catch (JSONException e) { e.printStackTrace(); Log.e("ParserStoreRecommed", "error"); } return null; }