Retrofit⽹络请求参数注解Json格式和form-data格式提交数据
(实⽤)
Retrofit 以Json格式提交数据 (模拟登录功能)
1、ApiService
/**
* 登录
*/
@Headers("Content-Type:application/json")
@POST("user/login")
Observable<ResponseBody> login(@Body RequestBody requestBody);
2、创建⼀个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转为json体
Gson gson = new Gson();
HashMap<String, String> map = new HashMap<>();
map.put("nickname", "123456");
map.put("password, "abcdefg");
String jsonBody = Json(map);
// 此时的jsonBody打印出来的结果
// {"password":"abcdefg","nickname":"123456"}
4、Retrofit请求头是否携带 如果请求带请求头 ⽤⽅法⼀,如果不带请求头 ⽤⽅法⼆,请求头需要添加的数据根据需求⾃⾏添加
1、不带请求头
private void useRetrofit(String url) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.ate())
.ate())
.build();
android retrofit
apiServices = ate(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 = quest();
Request request = wBuilder()
.addHeader("Content-Type", "application/json;charset=utf-8")
.build();
return chain.proceed(request);
}
});
OkHttpClient client = builder.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.ate())
.
ate())
.client(client)
.build();
apiServices = ate(ApiService.class);
}
5、⽹络请求的代码
RequestBody body = ate(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);
} catch (IOException e) {
e.printStackTrace();
}
}
});
R etr o fit 以fo r m-da ta格式提交数据 (模拟登录功能)
etro
1、ApiService
1、当数据量⼤的情况下⽤⽅法1 推荐此⽅法 处理数据时⽅便⼀些
/
**
* 登录
/
@Multipart
@POST("users/login")
Observable<ResponseBody> phoneLogin(@PartMap Map<String, RequestBody> requestBodyMap);
2、当数据量⼩的情况下⽤⽅法2
/*
* 登录
*/
@Multipart
@POST("users/login")
Call<JsonObject> phoneLogin(@Part("nickname") RequestBody username, @Part("password") RequestBody password);
2、转为form-data
/**
* 转换为 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 = ate(MediaType.parse("multipart/form-data"),
<(key) == null ? "" : (key));
requestBodyMap.put(key, requestBody);
}
return requestBodyMap;
}
3、Retrofit请求头是否携带 如果请求带请求头 ⽤⽅法⼀,如果不带请求头 ⽤⽅法⼆
1、不带请求头
private void useRetrofit(String url) {
Retrofit retrofit = new Retrofit.Builder()
.
baseUrl(url)
.ate())
.ate())
.build();
apiServices = ate(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 = quest();
Request request = wBuilder()
.addHeader("Content-Type", "application/json;charset=utf-8")
.build();
return chain.proceed(request);
}
});
OkHttpClient client = builder.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.
ate())
.ate())
.client(client)
.build();
apiServices = ate(ApiService.class);
}
4、⽹络请求的代码
Map<String, String> formMaildata = new HashMap<>();
formMaildata.put("nickname", "123456");
formMaildata.put("password", "abcdef");
Map<String, RequestBody> stringRequestBodyMap = generateRequestBody(formMaildata);
/
/此时stringRequestBodyMap打印出来的结果
//{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);
// Success((T) string);
} catch (IOException e) {
e.printStackTrace();
}
}
});
我总结的不是很全,这位博主介绍的⼗分详细;超级实⽤

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。