詳解Android中使用OkHttp發(fā)送HTTP的post請求的方法
HTTP POST 和 PUT 請求可以包含要提交的內(nèi)容。只需要在創(chuàng)建 Request 對象時,通過 post 和 put 方法來指定要提交的內(nèi)容即可。
HTTP POST 請求的基本示例:
public class PostString { public static void main(String[] args) throws IOException { OkHttpClient client = new OkHttpClient(); MediaType MEDIA_TYPE_TEXT = MediaType.parse("text/plain"); String postBody = "Hello World"; Request request = new Request.Builder() .url("http://www.baidu.com") .post(RequestBody.create(MEDIA_TYPE_TEXT, postBody)) .build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) { throw new IOException("服務(wù)器端錯誤: " + response); } System.out.println(response.body().string()); } }
以 String 類型提交內(nèi)容只適用于內(nèi)容比較小的情況。當請求內(nèi)容較大時,應(yīng)該使用流來提交。
Post方式提交流
以流的方式POST提交請求體。請求體的內(nèi)容由流寫入產(chǎn)生。這個例子是流直接寫入 Okio 的BufferedSink。你的程序可能會使用 OutputStream ,你可以使用 BufferedSink.outputStream() 來獲取。
public static final MediaType MEDIA_TYPE_MARKDOWN = MediaType.parse("text/x-markdown; charset=utf-8"); private final OkHttpClient client = new OkHttpClient(); public void run() throws Exception { RequestBody requestBody = new RequestBody() { @Override public MediaType contentType() { return MEDIA_TYPE_MARKDOWN; } @Override public void writeTo(BufferedSink sink) throws IOException { sink.writeUtf8("Numbers\n"); sink.writeUtf8("-------\n"); for (int i = 2; i <= 997; i++) { sink.writeUtf8(String.format(" * %s = %s\n", i, factor(i))); } } private String factor(int n) { for (int i = 2; i < n; i++) { int x = n / i; if (x * i == n) return factor(x) + " × " + i; } return Integer.toString(n); } }; Request request = new Request.Builder() .url("https://api.github.com/markdown/raw") .post(requestBody) .build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); System.out.println(response.body().string()); }
Post方式提交文件
以文件作為請求體是十分簡單的。
public static final MediaType MEDIA_TYPE_MARKDOWN = MediaType.parse("text/x-markdown; charset=utf-8"); private final OkHttpClient client = new OkHttpClient(); public void run() throws Exception { File file = new File("README.md"); Request request = new Request.Builder() .url("https://api.github.com/markdown/raw") .post(RequestBody.create(MEDIA_TYPE_MARKDOWN, file)) .build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); System.out.println(response.body().string()); }
Post方式提交表單
使用 FormEncodingBuilder 來構(gòu)建和HTML <form> 標簽相同效果的請求體。鍵值對將使用一種HTML兼容形式的URL編碼來進行編碼。
private final OkHttpClient client = new OkHttpClient(); public void run() throws Exception { RequestBody formBody = new FormEncodingBuilder() .add("search", "Jurassic Park") .build(); Request request = new Request.Builder() .url("https://en.wikipedia.org/w/index.php") .post(formBody) .build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); System.out.println(response.body().string()); }
Post方式提交分塊請求
MultipartBuilder 可以構(gòu)建復雜的請求體,與HTML文件上傳形式兼容。多塊請求體中每塊請求都是一個請求體,可以定義自己的請求頭。這些請求頭可以用來描述這塊請求,例如他的 Content-Disposition 。如果 Content-Length 和 Content-Type 可用的話,他們會被自動添加到請求頭中。
private static final String IMGUR_CLIENT_ID = "..."; private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png"); private final OkHttpClient client = new OkHttpClient(); public void run() throws Exception { // Use the imgur image upload API as documented at https://api.imgur.com/endpoints/image RequestBody requestBody = new MultipartBuilder() .type(MultipartBuilder.FORM) .addPart( Headers.of("Content-Disposition", "form-data; name=\"title\""), RequestBody.create(null, "Square Logo")) .addPart( Headers.of("Content-Disposition", "form-data; name=\"image\""), RequestBody.create(MEDIA_TYPE_PNG, new File("website/static/logo-square.png"))) .build(); Request request = new Request.Builder() .header("Authorization", "Client-ID " + IMGUR_CLIENT_ID) .url("https://api.imgur.com/3/image") .post(requestBody) .build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); System.out.println(response.body().string()); }
相關(guān)文章
如何使用Flutter實現(xiàn)58同城中的加載動畫詳解
這篇文章主要給大家介紹了關(guān)于如何使用Flutter實現(xiàn)58同城中加載動畫詳?shù)南嚓P(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Flutter具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-10-10Android shell命令行中過濾adb logcat輸出的方法
本文主要介紹Android shell命令行中過濾adb logcat輸出,這里詳細說明了shell 命令過濾logcat 輸出內(nèi)容,有需要的小伙伴可以參考下2016-08-08Android 實現(xiàn)會旋轉(zhuǎn)的餅狀統(tǒng)計圖實例代碼
這篇文章主要介紹了Android 實現(xiàn)會旋轉(zhuǎn)的餅狀統(tǒng)計圖實例代碼的相關(guān)資料,這里附有實例代碼及實現(xiàn)效果圖,需要的朋友可以參考下2016-12-12Android 廣播監(jiān)聽網(wǎng)絡(luò)狀態(tài)詳解及實例代碼
這篇文章主要介紹了Android 廣播監(jiān)聽網(wǎng)絡(luò)狀態(tài)詳解及實例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02Android基于ListView實現(xiàn)類似QQ空間的滾動翻頁與滾動加載效果
這篇文章主要介紹了Android基于ListView實現(xiàn)類似QQ空間的滾動翻頁與滾動加載效果,涉及ListView相關(guān)屬性與方法的操作技巧,需要的朋友可以參考下2016-08-08Android 自定義 View 中使用 Spannable的實例詳解
這篇文章主要介紹了Android 自定義 View 中使用 Spannable的相關(guān)知識,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05