Okhttp操作文档

Okhttp介绍

OkHttp是一个高效的Http客户端,有如下的特点:

  1. 支持HTTP2/SPDY黑科技
  2. socket自动选择最好路线,并支持自动重连
  3. 拥有自动维护的socket连接池,减少握手次数
  4. 拥有队列线程池,轻松写并发
  5. 拥有Interceptors轻松处理请求与响应(比如透明GZIP压缩,LOGGING)
  6. 基于Headers的缓存策略

注意如果使用jar需要导入以下两个包

1. okhttp3
2. okio

地址

http://square.github.io/okhttp/

get参数

// 01. 定义okhttp
OkHttpClient okHttpClient_get = new OkHttpClient();
// 02.请求体
Request request = new Request.Builder()
    .get()//get请求方式
    .url("http://10.0.3.2:8080/WebServiceTest/servlet/ServcieTest?name=sy")//网址
    .build();
    
// 03.执行okhttp

Response response = okHttpClient_get.newCall(request).execute();
// 打印数据
System.out.println(response.body().string());

post请求参数

 // 定义okhttp
OkHttpClient okHttpClient_post_kv = new OkHttpClient();

// 定义请求体
// 执行okhttp
RequestBody body = new FormBody.Builder()
                    .add("name", "sy")//添加参数体
                    .add("age", "18")
                    .build();
Request request = new Request.Builder()
    .post(body) //请求参数
    .url("http://10.0.3.2:8080/WebServiceTest/servlet/ServcieTest")
    .build();
Response response = okHttpClient_post_kv.newCall(request).execute();
System.out.println(response.body().string());

post请求json

OkHttpClient okHttpClient_post_json = new OkHttpClient();
String json = "{\n" + "    \"age\": \"18\",\n" + "    \"name\": \"sy\"\n" + "}";
RequestBody body =
    RequestBody.create(MediaType.parse("application/json;charset=utf-8"), json);
Request request = new Request.Builder()
    .post(body)
    .url("http://10.0.3.2:8080/WebServiceTest/servlet/ServcieTest")
    .build();
Response response = okHttpClient_post_json.newCall(request).execute();
System.out.println(response.body().string());

上传图片

OkHttpClient okHttpClient_upload = new OkHttpClient();
                        
File file = new File(Environment.getExternalStorageDirectory() + "/download", "file.txt");
RequestBody body = RequestBody.create(MediaType.parse("application/octet-stream"), file);
Request request = new Request.Builder()
    .post(body)
    .url("http://10.0.3.2:8080/WebServiceTest/servlet/ServcieTest")
    .build();
Response response = okHttpClient_upload.newCall(request).execute();
System.out.println(response.body().string());

下载图片

OkHttpClient okHttpClient_down = new OkHttpClient();
Request request =
    new Request.Builder()
                .get()
                .url("http://10.0.3.2:8080/WebServiceTest/p22.jpg")
                .build();
okHttpClient_down.newCall(request).enqueue(MainActivity.this);

 /**
 * 超时错误,服务器无响应
 * 
 * @param call
 * @param e
 */
@Override
public void onFailure(Call call, IOException e)
{

}

/**
 * 服务器响应
 * 
 * @param call
 * @param response
 * @throws IOException
 */
@Override
public void onResponse(Call call, Response response)
    throws IOException
{
    InputStream inputStream = response.body().byteStream();
    final BitmapDrawable bitmapDrawable = new BitmapDrawable(inputStream);
    
    runOnUiThread(new Runnable()
    {
        @Override
        public void run()
        {
            
            mIv_main_load_image.setImageDrawable(bitmapDrawable);
        }
    });
    
}

你可能感兴趣的:(Okhttp操作文档)