OkHttpUtils请求网络查询手机号码归属地

这里okhttp使用了OkHttpUtils,来源于洪洋大神的封装。
导入方法

AndroidStudio:
在build.gradle中引入

compile 'com.zhy:okhttputils:2.6.2'

因为同时要使用okhttp,必须还要引入

 compile 'com.squareup.okhttp3:okhttp:3.5.0'

Eclipse:
下载jar包

OkHttpUtils给我们提供了十多种类,包括常用的get、post请求,和表单提交,上传下载等功能,可以满足我们开发的基本需求了。

这里写了一个比较简单的网络访问,请求接口的demo,用的是聚合数据的手机号码归属地。

下面是完成的效果图。

在聚合中找到数据查看接口调用方法:


phone 就是我们从控件的到的手机号
key 是聚合固定的,在我们申请数据的时候就给生成了。

测试一下返回结果,用于生成model

JSON返回示例:
{
"resultcode":"200",
"reason":"Return Successd!",
"result":{
    "province":"浙江",
    "city":"杭州",
    "areacode":"0571",
    "zip":"310000",
    "company":"中国移动",
    "card":"移动动感地带卡"
}
}

然后我们可以用GsonFormat 快速生成model类

public class NewBeans {


    /**
     * resultcode : 200
     * reason : Return Successd!
     * result : {"province":"广东","city":"中山","areacode":"0760","zip":"528400","company":"移动","card":""}
     * error_code : 0
     */

    private String resultcode;
    private String reason;
    private ResultBean result;
    private int error_code;

    public String getResultcode() {
        return resultcode;
    }

    public void setResultcode(String resultcode) {
        this.resultcode = resultcode;
    }

    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }

    public ResultBean getResult() {
        return result;
    }

    public void setResult(ResultBean result) {
        this.result = result;
    }

    public int getError_code() {
        return error_code;
    }

    public void setError_code(int error_code) {
        this.error_code = error_code;
    }

    public static class ResultBean {


        /**
         * province : 广东
         * city : 中山
         * areacode : 0760
         * zip : 528400
         * company : 移动
         * card :
         */

        private String province;
        private String city;
        private String areacode;
        private String zip;
        private String company;
        private String card;

        public String getProvince() {
            return province;
        }

        public void setProvince(String province) {
            this.province = province;
        }

        public String getCity() {
            return city;
        }

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

        public String getAreacode() {
            return areacode;
        }

        public void setAreacode(String areacode) {
            this.areacode = areacode;
        }

        public String getZip() {
            return zip;
        }

        public void setZip(String zip) {
            this.zip = zip;
        }

        public String getCompany() {
            return company;
        }

        public void setCompany(String company) {
            this.company = company;
        }

        public String getCard() {
            return card;
        }

        public void setCard(String card) {
            this.card = card;
        }
    }
}

Activity

拼接Url地址

    private String utl_ok = "http://apis.juhe.cn/mobile/get?phone=";
    private String KEY = "&key=6e1ce94fe231e817fb31daec3b3084d0";
    private String URL;
......
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.httptest);
        ButterKnife.bind(this);
        URL = utl_ok + etHttp.getText() + KEY;

使用OkhttpUtils进行get请求
这里的key也可以用.addParams("Key",KEY)添加,.execute返回请求结果
得到的结果存放在model,然后直接用get方法解析展示。

 public void okhttpGet() {
        OkHttpUtils
                .get()
                .url(URL)
                .addParams("phone", etHttp.getText().toString())
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(okhttp3.Call call, Exception e, int id) {
                        UToasts.showShort(OkhttpRetrofitActivity.this, "请求失败:" + e.getMessage().toString());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Gson gson = new Gson();
                        NewBeans newBeans = gson.fromJson(response, NewBeans.class);
                        newBeans.getResult();
                        UToasts.showLong(OkhttpRetrofitActivity.this, newBeans.getReason());

                        if (newBeans.getResultcode().equals("200")) {
                            tv.setText(
                                    "省:" + newBeans.getResult().getProvince().toString()
                                            + "\n"
                                            + "市:" + newBeans.getResult().getCity().toString()
                                            + "\n"
                                            + "区号:" + newBeans.getResult().getAreacode()
                                            + "\n"
                                            + "运营商:" + newBeans.getResult().getCompany());
                        } else {
                            tv.setText("手机号码输入有误,查询失败");
                        }
                    }
                });

    }

post方法跟get用法一样。
源码都在github上,需要的可以去查看
地址:https://github.com/wapchief/android-CollectionDemo

如果要获取响应详情的话,可以用Fiddler抓包查看。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值