OKHTTP3 簡單使用 包含上傳檔案

2021-08-01 03:34:48 字數 1734 閱讀 1585

這是非同步post請求:

//這是建立乙個請求引數類

requestbody requestbodypost = new formbody.builder()

.add("seq", emclient.getinstance().getcurrentuser())

.build();

//這是設定請求 模式與一些基礎設定,比如請求時間等等

request requestpost = new request.builder()

.url(url.essential_information_post)

.post(requestbodypost)

.build();

client.newcall(requestpost).enqueue(new callback()

@override

public void onresponse(call

call, response response) throws ioexception

});這是非同步get請求:

logutils.e("ydp=url==",url);

//建立okhttpclient物件

//建立乙個request

//請求加入排程

call

.enqueue(new callback()

@override

public void onresponse(call

call, response response) throws ioexception

});這是上傳檔案並且攜帶引數

認真看改一下邏輯就可以多個上傳了

現在是單個

/**

* 檔案 上傳

*/protected

void

post_file(final string url, final mapmap, file file, final bitmap bitmap)

if (map != null)

}request request = new request.builder().url(url).post(requestbody.build()).tag(this).build();

// readtimeout("請求超時時間" , 時間單位);

client.newbuilder().readtimeout(5000, timeunit.milliseconds).build().newcall(request).enqueue(new callback()

@override

public

void

onresponse(call call, response response) throws ioexception

});}

各位我就這些事我用過的所以我貼出來–其他的沒有用過

OkHttp3簡單使用

複製 目前最新的穩定版可能已經不是3.11.0了,可以到官方github來檢視最新版本 github.com square okht get 預設就是get請求,可以不寫 複製 與get相比,post多了乙個請求體 requestbody 複製 string url 複製 okhttpclient ...

okHttp3的簡單使用

在gradle中新增包 compile com.squareup.okhttp3 okhttp 3.9.0 建立okhttpclient例項 okhttpclient client new okhttpclient 如果需要發起http請求 request request new request.b...

okhttp3的簡單使用

注 如果引入的為舊版本com.squareup.okhttp,則有些語法會不一樣 通過okhttpclient傳送乙個簡單的post請求 public static void postdemo 建立請求方式,可加 header name,value 設定請求頭引數 request request n...