Retrofit 以Json格式提交數(shù)據(jù) (模擬登錄功能)
1贸人、ApiService
/**
* 登錄
*/
@Headers("Content-Type:application/json")
@POST("user/login")
Observable<ResponseBody> login(@Body RequestBody requestBody);
2尘颓、創(chuàng)建一個Bean類
public class UserBean {
private String nickname;
private String password;
public UserBean(String nickname, String password) {
this.nickname = nickname;
this.password = password;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
3疯趟、將Bean類通過Gson轉(zhuǎn)為json體
Gson gson = new Gson();
HashMap<String, String> map = new HashMap<>();
map.put("nickname", "123456");
map.put("password, "abcdefg");
String jsonBody = gson.toJson(map);
// 此時的jsonBody打印出來的結(jié)果
// {"password":"abcdefg","nickname":"123456"}
4、Retrofit請求頭是否攜帶 如果請求帶請求頭 用方法一锡溯,如果不帶請求頭 用方法二赶舆,請求頭需要添加的數(shù)據(jù)根據(jù)需求自行添加
1、不帶請求頭
private void useRetrofit(String url) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
apiServices = retrofit.create(ApiService.class);
}
2祭饭、帶請求頭
private void useHeaderRetrofit(String url) {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
Request request = original.newBuilder()
.addHeader("Content-Type", "application/json;charset=utf-8")
.build();
return chain.proceed(request);
}
});
OkHttpClient client = builder.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(client)
.build();
apiServices = retrofit.create(ApiService.class);
}
5芜茵、網(wǎng)絡(luò)請求的代碼
RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), requestBody);
Observable<ResponseBody> responseBodyObservable = apiServices.login(body);
responseBodyObservable.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Observer<ResponseBody>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
Log.d("TAG", "e:" + e);
}
@Override
public void onNext(ResponseBody responseBody) {
try {
String string = responseBody.string();
Log.d("TAG", string);
callBack.onSuccess((T) string);
} catch (IOException e) {
e.printStackTrace();
}
}
});
Retrofit 以form-data格式提交數(shù)據(jù) (模擬登錄功能)
1、ApiService
1倡蝙、當(dāng)數(shù)據(jù)量大的情況下用方法1 推薦此方法 處理數(shù)據(jù)時方便一些
/**
* 登錄
/
@Multipart
@POST("users/login")
Observable<ResponseBody> phoneLogin(@PartMap Map<String, RequestBody> requestBodyMap);
2九串、當(dāng)數(shù)據(jù)量小的情況下用方法2
/*
* 登錄
*/
@Multipart
@POST("users/login")
Call<JsonObject> phoneLogin(@Part("nickname") RequestBody username, @Part("password") RequestBody password);
2、轉(zhuǎn)為form-data
/**
* 轉(zhuǎn)換為 form-data
*
* @param requestDataMap
* @return
*/
public static Map<String, RequestBody> generateRequestBody(Map<String, String> requestDataMap) {
Map<String, RequestBody> requestBodyMap = new HashMap<>();
for (String key : requestDataMap.keySet()) {
RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"),
requestDataMap.get(key) == null ? "" : requestDataMap.get(key));
requestBodyMap.put(key, requestBody);
}
return requestBodyMap;
}
3寺鸥、Retrofit請求頭是否攜帶 如果請求帶請求頭 用方法一猪钮,如果不帶請求頭 用方法二
1、不帶請求頭
private void useRetrofit(String url) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
apiServices = retrofit.create(ApiService.class);
}
2胆建、帶請求頭
private void useHeaderRetrofit(String url) {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
Request request = original.newBuilder()
.addHeader("Content-Type", "application/json;charset=utf-8")
.build();
return chain.proceed(request);
}
});
OkHttpClient client = builder.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(client)
.build();
apiServices = retrofit.create(ApiService.class);
}
4烤低、網(wǎng)絡(luò)請求的代碼
Map<String, String> formMaildata = new HashMap<>();
formMaildata.put("nickname", "123456");
formMaildata.put("password", "abcdef");
Map<String, RequestBody> stringRequestBodyMap = generateRequestBody(formMaildata);
//此時stringRequestBodyMap打印出來的結(jié)果
//{password=abcdef, nickname=123456}
Observable<ResponseBody> responseBodyObservable = apiServices.login(stringRequestBodyMap);
responseBodyObservable.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Observer<ResponseBody>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
Log.d("TAG", "e:" + e);
}
@Override
public void onNext(ResponseBody responseBody) {
try {
String string = responseBody.string();
Log.d("TAG", string);
// callBack.onSuccess((T) string);
} catch (IOException e) {
e.printStackTrace();
}
}
});
我總結(jié)的不是很全,這位博主介紹的十分詳細(xì)笆载;超級實(shí)用