美文网首页Android知识Android技术知识Android开发
OkHttpUtils请求网络查询手机号码归属地

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

作者: 八怪不姓丑 | 来源:发表于2017-03-07 11:04 被阅读146次

    这里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抓包查看。

    相关文章

      网友评论

        本文标题:OkHttpUtils请求网络查询手机号码归属地

        本文链接:https://www.haomeiwen.com/subject/ywyugttx.html