OkHttp和Volley

OkHttp和Volley

okhttp

//首先导入依赖
implementation ‘com.squareup.okhttp3:okhttp:4.0.0-alpha02’
//代码

 /**
     * okhttp:第三方下载框架
     * get:
     * 同步--线程:
     * 异步:
     * post:
     * */
    public void okhttp_get(String urlstr){
        //同步GET
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(5000, TimeUnit.MILLISECONDS);//设置连接超时
        builder.readTimeout(4,TimeUnit.MINUTES);//分钟

        OkHttpClient client = builder.build();//客户端

        Request.Builder builder1 = new Request.Builder();
        builder1.url(urlstr);//访问的网址
        Request request = builder1.build();
//        new Request.Builder().url(urlstr).get().build();

        Call call = client.newCall(request);
        Response response;
        try {
            response = call.execute();
            String result=response.body().string();//拿到json串
            Log.e("###",result);
        } catch (Exception e) {
            e.printStackTrace();
        }

//        Request request = new Request.Builder().url(urlstr).get().build();
//        OkHttpClient client = new OkHttpClient();
//        try {
//            Response response1 = client.newCall(request).execute();
//            if(response1.isSuccessful()){
//                String string = response1.body().string();
//
//            }else {
//
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

    }

    /**
     * 同步的post
     * */
    public void okhttp_post(String urlstr){
        //同步POST
//        OkHttpClient.Builder builder2 = new OkHttpClient.Builder();
//        OkHttpClient client = builder2.build();
//
//        Request.Builder builder1 = new Request.Builder();
//        builder1.url(urlstr);
//
//        FormBody.Builder builder =new FormBody.Builder();
//        builder.add("phone","11111111111");
//        builder.add("password","123");
//        FormBody body = builder.build();
//
//        builder1.post(body);
//
//        Request request = builder1.build();
//        Call call = client.newCall(request);
//
//        Response response;
//        try {
//             response = call.execute();
//            String string = response.body().string();
//            Log.e("###",string);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }


        FormBody body =new FormBody.Builder()
                .add("phone","11111111111")
                .add("passwd","123")
                .build();
        Request request=new Request.Builder().url(urlstr).post(body).build();
        OkHttpClient client=new OkHttpClient();
        try {
            Response response = client.newCall(request).execute();
            if(response.isSuccessful()){
                String string = response.body().string();
                Log.e("###",string);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 异步GET
     * */
    public void asyncGet(String urlstr){
        //获得请求对象
        final Request request=new Request.Builder().url(urlstr).get().build();
        OkHttpClient client=new OkHttpClient();//获得客户端对象
        Call call = client.newCall(request);
        call.enqueue(new okhttp3.Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String string = response.body().string();
                Log.e("###",string);
            }
        });
    }

    /**
     * 异步POST*/
    public void asyncPost(String urlstr){
        OkHttpClient client = new OkHttpClient();
        FormBody body = new FormBody.Builder()
                .add("", "")
                .add("", "")
                .build();

        final Request request = new Request.Builder().url(urlstr).post(body).build();

        Call call = client.newCall(request);
        call.enqueue(new okhttp3.Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                //失败
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                //成功
                Log.e("###",response.body().string());
            }
        });
    }

Volley

//适用于频率高的小数据的下载量
//同上,导入依赖
implementation ‘eu.the4thfloor.volley:com.android.volley:2015.05.28’
//代码

 public void volleyGet(String urlstr, Context context){
        RequestQueue requestQueue= Volley.newRequestQueue(context);//获得volley队列
        StringRequest stringRequest = new StringRequest(StringRequest.Method.GET,urlstr,listener,errorListener);
        requestQueue.add(stringRequest);//将请求添加到队列

        requestQueue.start();//启动队列
    }
    com.android.volley.Response.Listener<String> listener=new com.android.volley.Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            //取参数做为结果
            Log.e("###",response);
        }
    };
    com.android.volley.Response.ErrorListener  errorListener=new com.android.volley.Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    };

    public void volleyPost(String urlstr, Context context){
        RequestQueue requestQueue= Volley.newRequestQueue(context);//获得volley队列
        StringRequest request = new StringRequest(StringRequest.Method.POST, urlstr,listener,errorListener){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map=new HashMap<>();

                map.put("phone","aa");
                map.put("passwd","ddd");
                return map;
            }
        };
        requestQueue.add(request);
        requestQueue.start();
    }
    public void VolleyImage(String urlstr, Context context, final ImageView imageView){
        ImageRequest imageRequest=new ImageRequest(urlstr, new com.android.volley.Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                imageView.setImageBitmap(response);
            }
        }, 100, 100, Bitmap.Config.ARGB_8888, errorListener);
        RequestQueue requestQueue= Volley.newRequestQueue(context);//获得volley队列
        requestQueue.add(imageRequest);
        requestQueue.start();
    }

你可能感兴趣的:(OkHttp和Volley)