您好,登錄后才能下訂單哦!
使用okhttp來調(diào)用百度提供的頻道新聞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)); } }); } }); }
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。