Android的改造2自定义JSON请求
我在我使用改装2.开发一个应用程序 我的要求JSON是如下Android的改造2自定义JSON请求
{
"FirstName":"ABC",
"LastName":"XYZ",
"Email":"[email protected]",
"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);
}
}
我的改造客户端类如下:
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;
}
}
下面就是我得到错误 Web服务的调用代码。
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);
}
});
感谢您的回复。我认为你的答案正在工作,但现在我得到了java.net。SocketTimeoutException。 –
你可以试试https://*.com/questions/39219094/sockettimeoutexception-in-retrofit –
感谢兄弟。它的工作现在。 –
您应该提供带有错误的堆栈跟踪。通过浏览代码我可以看到的一件事是SignUpRequestParent构造函数接受SignUpRequestChild对象的列表,而您在进行api调用时传递一个新的SignUpRequestChild对象。
我想你调用了错误的方式,试试这个
在RetrofitClient类中添加这
public static APIService getService() {
return getClient().create(APIService.class);
}
然后将它从任何地方
RetrofitClient.getService.someMethodHere().enqueue ...
可否请你用错误添加堆栈跟踪? –
也SignUpResponseModel定义 –
@cgomezmendez - 我无法打电话。编码时的错误是:将第11个参数投射到Java.util.List。或者从List更改为SignUpRequestChild的方法'SignUpRequestParent'的第11个参数。 –