Json获取与解析

package com.bo.test;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.google.gson.Gson;
import com.show.api.ShowApiRequest;

public class MainActivity extends AppCompatActivity {
    protected Handler mHandler = new Handler();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.json);
        final EditText txt = (EditText) this.findViewById(R.id.json);
        Button myBtn = (Button) this.findViewById(R.id.btn1);
        myBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                new Thread() {
                    //在新线程中发送网络请求
                    public void run() {
                        String appid = "42449";
                        String secret = "3b49aa0827d94ca59a6d8127a52565c5";
                        final String res = new ShowApiRequest("http://route.showapi.com/6-1", appid, secret)
                                .addTextPara("num", "13151569013")
                                .post();
//                        System.out.println(res);
                        Gson gson = new Gson();
                        final Bean bean = gson.fromJson(res,Bean.class);
                        System.out.println(bean.getShowapi_res_body().getName());
                        //把返回内容通过handler对象更新到界面
                        mHandler.post(new Thread() {
                            public void run() {
                                txt.setText(bean.getShowapi_res_body().getCity());
                            }
                        });
                    }
                }.start();


            }
        });
    }
}

//    String appid = "42449";
//    String secret = "3b49aa0827d94ca59a6d8127a52565c5";
//    获取网站返回的JSON数据
//    final String res = new ShowApiRequest("http://route.showapi.com/109-35", appid, secret)
//            .addTextPara("channelId", "5572a108b3cdc86cf39001ce")
//            .addTextPara("channelName", "")
//            .addTextPara("title", "")
//            .addTextPara("page", "1")
//            .addTextPara("needContent", "0")
//            .addTextPara("needHtml", "0")
//            .addTextPara("needAllList", "0")
//            .addTextPara("maxResult", "20")
//            .post();

Bean:
这里写图片描述

http://www.bejson.com/json2javapojo/

package com.bo.test;

/**
 * Created by bo on 2017/7/18.
 */

public class Bean {
    private int showapi_res_code;

    private String showapi_res_error;
    private Showapi_res_body showapi_res_body;

    //=========================内部类(要定义成public)
    public class Showapi_res_body {
        private int num;

        private String prov;

        private int ret_code;

        private String areaCode;

        private String name;

        private String cityCode;

        private String postCode;

        private String provCode;

        private int type;

        private String city;

        public void setNum(int num) {
            this.num = num;
        }

        public int getNum() {
            return this.num;
        }

        public void setProv(String prov) {
            this.prov = prov;
        }

        public String getProv() {
            return this.prov;
        }

        public void setRet_code(int ret_code) {
            this.ret_code = ret_code;
        }

        public int getRet_code() {
            return this.ret_code;
        }

        public void setAreaCode(String areaCode) {
            this.areaCode = areaCode;
        }

        public String getAreaCode() {
            return this.areaCode;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getName() {
            return this.name;
        }

        public void setCityCode(String cityCode) {
            this.cityCode = cityCode;
        }

        public String getCityCode() {
            return this.cityCode;
        }

        public void setPostCode(String postCode) {
            this.postCode = postCode;
        }

        public String getPostCode() {
            return this.postCode;
        }

        public void setProvCode(String provCode) {
            this.provCode = provCode;
        }

        public String getProvCode() {
            return this.provCode;
        }

        public void setType(int type) {
            this.type = type;
        }

        public int getType() {
            return this.type;
        }

        public void setCity(String city) {
            this.city = city;
        }

        public String getCity() {
            return this.city;
        }

        // 重写toString()方法
        @Override
        public String toString() {
            return "location{" +
                    "省份='" + prov + '\'' +
                    ", 运营商='" + name + '\'' +
                    ", 城市='" + city + '\'' +
                    '}';
        }
    }

    //==============================底下是外部类
    public void setShowapi_res_code(int showapi_res_code) {
        this.showapi_res_code = showapi_res_code;
    }

    public int getShowapi_res_code() {
        return this.showapi_res_code;
    }

    public void setShowapi_res_error(String showapi_res_error) {
        this.showapi_res_error = showapi_res_error;
    }

    public String getShowapi_res_error() {
        return this.showapi_res_error;
    }

    public void setShowapi_res_body(Showapi_res_body showapi_res_body) {
        this.showapi_res_body = showapi_res_body;
    }

    public Showapi_res_body getShowapi_res_body() {
        return this.showapi_res_body;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值