OkHttp 和Volley

OkHttp 导入依赖

  implementation 'com.squareup.okhttp3:okhttp:3.12.1'

okhttp是一个第三方类库,用于android中请求网络。
这是一个开源项目,是安卓端最火热的轻量级框架,由移动支付Square公司贡献(该公司还贡献了Picasso和LeakCanary)
。用于替代HttpUrlConnection和Apache HttpClient(android API23
里已移除HttpClient)。

  • 1.同步get请求:开启子线程
    2.同步post请求:开启子线程
    3.异步get请求:不需要开启子线程
    4.异步post请求:不需要开启子线程

OkHttp的post/get和异步

1. public void OKHttp_post(final String urlPath){ new Thread(new
Runnable() {
@Override
public void run() {
FormBody formBody = new FormBody.Builder().add(“phone”, “wuyancu”).add(“passwd”, “wert”).build();
Request request = new Request.Builder().url(urlPath).post(formBody).build();
OkHttpClient client = new OkHttpClient();

           try {
               String s = client.newCall(request).execute().body().toString();
               Log.e("OkHttp_post",s);
           } catch (IOException e) {
               e.printStackTrace();
           }
       }    }).start(); }

2. public void OkHttp_get(final String urlPath){ new Thread(new
Runnable() {
@Override
public void run() {
Request request = new Request.Builder().url(urlPath).get().build();
OkHttpClient client = new OkHttpClient();

           try {
               Response response = client.newCall(request).execute();
               String string = response.body().string();
               Log.e("OKHttp_get",string);

           } catch (IOException e) {
               e.printStackTrace();
           }
       }    }).start(); }

3. public void OkHttp_Asyn_Post(String urlPath){
FormBody body = new FormBody.Builder().add(“phone”,“222”).add(“password”,“sadnuas”).build();
Request request = new Request.Builder().url(urlPath).build();
OkHttpClient client = new OkHttpClient();
Call call =client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {

            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.e("OkHttp_Asyn_Post",response.body().toString());
            }
        });
    }
  **4**.  public void OkHttp_Asyn_Get(String urlPath){
        final Request request = new Request.Builder().url(urlPath).get().build();
        OkHttpClient client = new OkHttpClient();
        Call call =client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
              Log.e("OkHttp_Asyn_Get",response.body().toString());
            }
        });
    }

Volley

导入依赖

implementation 'com.android.volley:volley:1.1.0'



所谓Volley,它是2013年Google I/O上发布的一款网络框架,基于Android平台,能使网络通信更快,更简单,更健全

优点:
(1)默认Android2.3及以上基于HttpURLConnection,2.3以下使用基于HttpClient;
(2)符合Http 缓存语义 的缓存机制(提供了默认的磁盘和内存等缓存);
(3)请求队列的优先级排序;
(4)提供多样的取消机制;
(5)提供简便的图片加载工具(其实图片的加载才是我们最为看重的功能);(6)一个优秀的框架。

缺点:
它只适合数据量小,通信频繁的网络操作,如果是数据量大的,像音频,视频等的传输,还是不要使用Volley的为好
--------------------- 

1. public void VolleyImage(String urlstr, Context context, final
ImageView imageView) {
ImageRequest imageRequest = new ImageRequest(urlstr, new com.android.volley.Response.Listener() {
@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();
}
2. public void Volley_post(final String urlPath,Context context){
StringRequest request = new StringRequest(StringRequest.Method.POST, urlPath, listener,
errorListener) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> map = new HashMap<>();
map.put(“phone”, “10091661”);
map.put(“passwd”, “169”);
return map;
}

        };
        queue.add(request);
        queue.start();
    }

3. public void Volley_get(final String urlPath, Context context){
queue= Volley.newRequestQueue(context);
StringRequest request = new StringRequest(StringRequest.Method.GET, urlPath, listener,
errorListener);
queue.add(request);
queue.start();
}
com.android.volley.Response.ErrorListener errorListener=new com.android.volley.Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

        }
    };
    com.android.volley.Response.Listener<String> listener=new com.android.volley.Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.e("Volley_Get",response);
        }
    };
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值