調用百度api微博熱門精選介面,使用了volley,簡單說說volley get的請求方式的使用 header的設置和請求參數的設置,見代碼如下: ...
調用百度api微博熱門精選介面,使用了volley,簡單說說volley get的請求方式的使用
header的設置和請求參數的設置,見代碼如下:
private void getWeixinNews() { StringBuilder sb = new StringBuilder(); try { sb.append("?num=").append(10).append("&rand=").append(1).append("&word=").append((URLEncoder.encode("西游記","UTF-8")).toString()). append("&page=").append(1).append("&src=").append((URLEncoder.encode("人民日報","UTF-8")).toString()); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } stringRequest = new StringRequest(Request.Method.GET, "http://apis.baidu.com/txapi/weixin/wxhot"+sb.toString(), new Response.Listener<String>() { @Override public void onResponse(String response) { Log.d("TAG", response); parseJson(response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e("TAG", error.getMessage(), error); } }) { @Override public Map<String, String> getHeaders() throws AuthFailureError { Map<String, String> headers = new HashMap<String, String>(); headers.put("Charset", "UTF-8"); headers.put("Content-Type", "application/x-javascript"); headers.put("Accept-Encoding", "gzip,deflate"); headers.put("apikey", "f8072b317a936623251258810df09d4e"); return headers; } }; } private void parseJson(String response) { Gson gson = new Gson(); BigWeiXin bigWeiXin = gson.fromJson(response, BigWeiXin.class); List<WeiXin> newslist = bigWeiXin.getNewslist(); for (WeiXin weixin : newslist) { Log.d("TAG", "ctime is " + weixin.getCtime()); Log.d("TAG", "title is " + weixin.getTitle()); Log.d("TAG", "description is " + weixin.getDescription()); Log.d("TAG", "picUrl is " + weixin.getPicUrl()); Log.d("TAG", "url is " + weixin.getUrl()); } }