程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> android volley get請求使用,androidvolley

android volley get請求使用,androidvolley

編輯:關於.NET

android volley get請求使用,androidvolley


調用百度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());
        }
    }

  

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved