日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

android get请求最长字符,Android OKHTTP3的GET和POST方法(带basic auth)

發(fā)布時間:2023/11/27 生活经验 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android get请求最长字符,Android OKHTTP3的GET和POST方法(带basic auth) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

使用前需要在Gradle Script中的build gradle中引入:

compile 'com.squareup.okio:okio:1.13.0'

compile 'com.squareup.okhttp3:okhttp:3.9.0'

GET

//創(chuàng)建OkHttpClient對象

OkHttpClient okHttpClient = new OkHttpClient.Builder()

.connectTimeout(10, TimeUnit.SECONDS)

.writeTimeout(10,TimeUnit.SECONDS)

.readTimeout(20, TimeUnit.SECONDS)

.build();

final Request request = new Request.Builder()

.url("http://172.20.192.168:8080/getbookByFrom?name=android基礎&price=50")//請求的url

.get()//設置請求方式,get()/post() 查看Builder()方法知,在構建時默認設置請求方式為GET

.build(); //構建一個請求Request對象

//創(chuàng)建/Call

Call call = okHttpClient.newCall(request);

//加入隊列 異步操作

call.enqueue(new Callback() {

//請求錯誤回調(diào)方法

@Override

public void onFailure(Call call, IOException e) {

System.out.println("連接失敗");

}

//異步請求(非主線程)

@Override

public void onResponse(Call call, Response response) throws IOException {

if(response.code()==200) {

System.out.println(response.body().string());

}

});

POST(json方式)

OkHttpClient okHttpClient = new OkHttpClient.Builder()

.connectTimeout(10, TimeUnit.SECONDS)

.writeTimeout(10,TimeUnit.SECONDS)

.readTimeout(20, TimeUnit.SECONDS)

.build();

Book book = new Book();

book.setName("android基礎");

book.setPrice(59);

//使用Gson 添加 依賴 compile 'com.google.code.gson:gson:2.8.1'

Gson gson = new Gson();

//使用Gson將對象轉換為json字符串

String json = gson.toJson(book);

//MediaType 設置Content-Type 標頭中包含的媒體類型值

RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8")

, json);

Request request = new Request.Builder()

.url("http://172.20.192.168:8080/getbookByJson")//請求的url

.post(requestBody)

.build();

//創(chuàng)建/Call

Call call = okHttpClient.newCall(request);

//加入隊列 異步操作

call.enqueue(new Callback() {

//請求錯誤回調(diào)方法

@Override

public void onFailure(Call call, IOException e) {

System.out.println("連接失敗");

}

@Override

public void onResponse(Call call, Response response) throws IOException {

System.out.println(response.body().string());

}

});

關于將對象轉換為json字符串,除了使用Gson還可以用JSONObject:

public static final MediaType JSON=MediaType.parse("application/json; charset=utf-8");

JSONObject jsonObject = new JSONObject();

try {

//example: {'likes':['體育','政治'...]}

jsonObject.put("likes",selectedThemes);

} catch (JSONException e) {

e.printStackTrace();

}

//創(chuàng)建一個RequestBody(參數(shù)1:數(shù)據(jù)類型 參數(shù)2傳遞的json串)

RequestBody requestBody = RequestBody.create(JSON, jsonObject.toString());

開發(fā)過程中遇到傳json需要帶上authentication的問題,然后在Stack Overflow上找到了解決方法——使用Interceptor。

import java.io.IOException;

import okhttp3.Credentials;

import okhttp3.Interceptor;

import okhttp3.Request;

import okhttp3.Response;

public class BasicAuthInterceptor implements Interceptor {

private String credentials;

public BasicAuthInterceptor(String user, String password) {

this.credentials = Credentials.basic(user, password);

}

@Override

public Response intercept(Chain chain) throws IOException {

Request request = chain.request();

Request authenticatedRequest = request.newBuilder()

.header("Authorization", credentials).build();

return chain.proceed(authenticatedRequest);

}

}

這樣就把username和password放進了authentication里。然后把這個Interceptor加到OkHttpClient就可以了。

OkHttpClient client = new OkHttpClient.Builder()

.addInterceptor(new BasicAuthInterceptor(username, password))

.build();

總結

以上是生活随笔為你收集整理的android get请求最长字符,Android OKHTTP3的GET和POST方法(带basic auth)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。