使用okhttp來(lái)調(diào)用百度提供的頻道新聞API(http://apistore.baidu.com/apiworks/servicedetail/688.html)
站在用戶的角度思考問(wèn)題,與客戶深入溝通,找到漢陰網(wǎng)站設(shè)計(jì)與漢陰網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站制作、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名注冊(cè)、虛擬主機(jī)、企業(yè)郵箱。業(yè)務(wù)覆蓋漢陰地區(qū)。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"); //可以省略,默認(rèn)是GET請(qǐng)求 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)); } }); } }); }
創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國(guó)云服務(wù)器,動(dòng)態(tài)BGP最優(yōu)骨干路由自動(dòng)選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機(jī)房獨(dú)有T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進(jìn)行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動(dòng)現(xiàn)已開(kāi)啟,新人活動(dòng)云服務(wù)器買多久送多久。