okhttp的get访问(加header)-创新互联

使用okhttp来调用百度提供的频道新闻API(http://apistore.baidu.com/apiworks/servicedetail/688.html)

站在用户的角度思考问题,与客户深入沟通,找到汉阴网站设计与汉阴网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:网站制作、网站建设、企业官网、英文网站、手机端网站、网站推广、域名注册虚拟主机、企业邮箱。业务覆盖汉阴地区。

okhttp的添加源

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

 String httpUrl = String httpArg = String requestString=requestString=httpUrl++httpArgrequest(requestString)

public void request(String requestString) {
    OkHttpClient mOkHttpClient=new OkHttpClient();
    Request.Builder requestBuilder = new Request.Builder().url(requestString).addHeader("apikey","592e46b62cfe201c68bf7d9f18db11ee");
    //可以省略,默认是GET请求
    requestBuilder.method("GET",null);
    Request request = requestBuilder.build();
    Call mcall= mOkHttpClient.newCall(request);
    mcall.enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            System.out.println("失败");
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    swipeRefreshLayout.setRefreshing(false);
                }
            });
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            if (null != response.cacheResponse()) {
                String str = response.cacheResponse().toString();
                Log.i("wangshu", "cache---" + str);
            } else {
                String string=response.body().string().toString();
                System.out.println(string);
                try {
                    System.out.println(1);
                    JSONObject jsonObject=new JSONObject(string);
                    JSONArray jsonArray=jsonObject.getJSONObject("showapi_res_body").getJSONObject("pagebean").getJSONArray("contentlist");
                    for (int i=0;i<=9;i++){
                        XinWenBean xinWenBean=new XinWenBean();
                        xinWenBean.setTitle(jsonArray.getJSONObject(i).getString("title"));
                        xinWenBeanList.add(xinWenBean);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String str = response.networkResponse().toString();
                Log.i("wangshu", "network---" + str);
            }
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    swipeRefreshLayout.setRefreshing(false);
                    recyclerView.setAdapter(new XinWenRecyclerViewAdapter(getActivity(),xinWenBeanList));
                }
            });
        }
    });
}

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


文章题目:okhttp的get访问(加header)-创新互联
文章路径:http://abwzjs.com/article/dpgheh.html