Volley第三方请求网络数据

Volley架包: https://github.com/mcxiaoke/android-volley.git
数据来源:聚合数据

/**
 * Created by Libin don 2016/7/16.
 * Voelly请求网络数据,工具类
 *
 */
public class NetUtile {

    private final RequestQueue queue;

    //        1.创建RequestQueue
    public NetUtile(Context context) {
        queue = Volley.newRequestQueue(context);
    }

    //        2.创建StringRequest,get请求
    public void RequestQueue_Get(String url) {

        /*
        * StringQueue 中的三种参数:
        *                       1请求路径;
        *                       2请求成功;
        *                       3请求失败;
        *
        * */
        StringRequest sr = new StringRequest("http://apis.juhe.cn/goodbook/catalog?key=3e100e799d37b42ff2cdbecc67ecb12b&dtype=json", new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d("MainActivity", response.toString());
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("MainActivity", "请求失败!!!");

            }
        });
        queue.add(sr);
    }

    //        2.创建StringRequest,post请求
    public void RequestQueue_Post(String url) {
        StringRequest psr = new StringRequest(Request.Method.POST, "http://v.juhe.cn/toutiao/index", new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d("MainActivity", response.toString());
            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("MainActivity", "请求失败!!!");

            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<>();
                map.put("key", "a4806d02149387aaeb859a73888a081c");
                map.put("type", "top");
                return map;
            }
        };
        queue.add(psr);
    }
    /* 网络请求第二种方法:
                           1.JsonObjectRequest 请求一段JSON数据
                           2.JsonArrayRequest  请求一段JSON数组数据
       */
       /* JsonObjectRequest 中的三个参数(Get请求):
       *                                           1.请求路径
       *                                           2.请求成功
       *                                           3.请求失败
       * */
// 1.JsonObjectRequest 请求一段JSON数据 ,Get请求
    public void JsonObjectRequest_Get(String url){
        JsonObjectRequest jor=new JsonObjectRequest("http://apis.juhe.cn/goodbook/catalog?key=3e100e799d37b42ff2cdbecc67ecb12b&dtype=json", new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {

                Log.d("MainActivity", response.toString());
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("MainActivity","请求失败!!!" );
            }
        });
        queue.add(jor);
    }
// 2.JsonObjectRequest 请求一段JSON数据 ,Post请求
     /* JsonObjectRequest 中的四个参数(Post请求):
                                                   1.请求方式
       *                                           2.请求路径
       *                                           3.请求成功
       *                                           4.请求失败
       *     由于是post请求,所以我们要重写getParams();
       * */
    public void JsonObjectRequest_Post(String url){

        Map<String, String> map=new HashMap<>();
        map.put("key", "a4806d02149387aaeb859a73888a081c");
        map.put("type", "top");
        JSONObject jsonobject=new JSONObject(map);
        final String mRequestBody = appendParameter(url,map);
        JsonObjectRequest pjor=new JsonObjectRequest(Request.Method.POST, "http://v.juhe.cn/toutiao/index",jsonobject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Log.d("MainActivity", response.toString());
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }){
            @Override
            public String getBodyContentType() {
                return "application/x-www-form-urlencoded; charset=" + getParamsEncoding();
            }
            @Override
            public byte[] getBody() {
                try {
                    return mRequestBody == null ? null : mRequestBody.getBytes(PROTOCOL_CHARSET);
                } catch (UnsupportedEncodingException uee) {
                    VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
                            mRequestBody, PROTOCOL_CHARSET);
                    return null;
                }
            }
        };

        queue.add(pjor);
    }
    private String appendParameter(String url,Map<String,String> params){
        Uri uri = Uri.parse(url);
        Uri.Builder builder = uri.buildUpon();
        for(Map.Entry<String,String> entry:params.entrySet()){
            builder.appendQueryParameter(entry.getKey(),entry.getValue());
        }
        return builder.build().getQuery();
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值