zoukankan      html  css  js  c++  java
  • 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; }
  • 相关阅读:
    虚拟机中无法显示摄像头驱动问题
    格式化的打印输出
    adb 常用命令
    stl list 正确删除节点程序实例
    程序猿的终点?!!!
    奇迹是否会发生?乙肝从大三阳到小三阳到自愈!
    2014年誓言:干掉网页设计程序——Dreamweaver!
    Dreamweaver杀手!Illustrator终结者?Flash的末日?图形图像设计程序之网页版
    网页程序 vs 桌面程序
    x13 vs md5
  • 原文地址:https://www.cnblogs.com/maxiaodoubao/p/3168870.html
Copyright © 2011-2022 走看看