gpt4 book ai didi

Android Retrofit 2 自定义 JSON 请求

转载 作者:太空狗 更新时间:2023-10-29 13:51:40 25 4
gpt4 key购买 nike

我正在开发一个应用程序,我正在使用改造 2。我的请求JSON如下

{
"FirstName":"ABC",
"LastName":"XYZ",
"Email":"abc@xyz.com",
"Password":123456",
"PhoneNo":"1234567890",
"Country":"1",
"State":"1",
"City":"1",
"Town":"YYYYYY",
"Details":[{
"Education":"BE",
"Sports":"Cricket",
"Hobby":"TV"
}]
}

我的API服务调用如下

public interface APIService {
@POST("/signup")
Call<SignUpResponseModel> signUp(@Body SignUpRequestParent body);
}

我的 API Util 类如下

public class ApiUtils {
private ApiUtils() {}
public static final String BASE_URL = "http://URL/";

public static APIService getAPIService() {
return RetrofitClient.getClient(BASE_URL).create(APIService.class);
}
}

我的Retrofit客户端类如下:

public class RetrofitClient {
private static Retrofit retrofit = null;
public static Retrofit getClient(String baseUrl) {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.client(getHeader())
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}

public static OkHttpClient getHeader() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okClient = new OkHttpClient.Builder()
.addInterceptor(interceptor)
.addNetworkInterceptor(new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request request = null;
Request original = chain.request();
// Request customization add request headers
Request.Builder requestBuilder = original.newBuilder()
.addHeader("Authorization", "auth")
request = requestBuilder.build();
return chain.proceed(request);
}
}).build();
return okClient;
}
}

我的请求模型类如下:

public class SignUpRequestParent {
final String FirstName;
final String LastName;
final String Email;
final String Password;
final String PhoneNo;
final String Country;
final String State;
final String City;
final String Town;
final List<SignUpRequestChild> Details;
public SignUpRequestParent(String FirstName, String LastName, String Email, String Password, String PhoneNo, String Country, String State, String City, String Town, List<SignUpRequestChild> Details) {
this.FirstName = FirstName;
this.LastName = LastName;
this.Email = Email;
this.Password = Password;
this.PhoneNo = PhoneNo;
this.Country = Country;
this.State = State;
this.City = City;
this.Town = Town;
this.Details = Details;
}
}

public class SignUpRequestChild {
final String Education;
final String Sports;
final String Hobby;

public SignUpRequestChild(String Education, String Sports, String Hobby) {
this.Education = Education;
this.Sports = Sports;
this.Hobby = Hobby;
}
}

下面是我遇到错误的网络服务调用代码。

mAPIService.signUp(new SignUpRequestParent(name, surName, email, password, mobileNumber, country, state, city, Town, new SignUpRequestChild(Education,Sports,Hobby))).enqueue(new Callback<SignUpResponseModel>() {
@Override
public void onResponse(Call<SignUpResponseModel> call, Response<SignUpResponseModel> response) {
if(response.isSuccessful()) {
Log.e("SignUpResponse", response.body().toString());
}
}
@Override
public void onFailure(Call<SignUpResponseModel> call, Throwable t) {
Log.e("RetroFit", ""+t);
}
});

调用代码的第一行给出了错误。因为我不知道如何从类中创建嵌套的 JSON 数组。

请告诉我该怎么做。我哪里错了。

提前谢谢你。

最佳答案

所以我认为你的问题是你正在这样做

mAPIService.signUp(new SignUpRequestParent(name, surName,
email, password, mobileNumber, country, state, city,
Town, new SignUpRequestChild(Education,Sports,Hobby))).enqueue(new Callback<SignUpResponseModel>() {
@Override
public void onResponse(Call<SignUpResponseModel> call,
Response<SignUpResponseModel> response) {
if(response.isSuccessful()) {
Log.e("SignUpResponse", response.body().toString());
}
}
@Override
public void onFailure(Call<SignUpResponseModel> call, Throwable t) {
Log.e("RetroFit", ""+t);
}
});

但是你的

new SignUpRequestChild(Education,Sports,Hobby))

根据你的构造函数,它应该是一个列表,所以你真的应该这样做

List childs = new ArrayList<SignUpRequestChild>();
childs.add(new SignUpRequestChild(Education,Sports,Hobby)));
mAPIService.signUp(new SignUpRequestParent(name, surName,
email, password, mobileNumber, country, state, city,
Town, childs).enqueue(new Callback<SignUpResponseModel>() {
@Override
public void onResponse(Call<SignUpResponseModel> call,
Response<SignUpResponseModel> response) {
if(response.isSuccessful()) {
Log.e("SignUpResponse", response.body().toString());
}
}
@Override
public void onFailure(Call<SignUpResponseModel> call, Throwable t) {
Log.e("RetroFit", ""+t);
}
});

关于Android Retrofit 2 自定义 JSON 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46238284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com