Android Retrofit 2自定义JSON请求

时间:2017-09-15 11:15:02

标签: android json retrofit retrofit2

我正在开发一个应用程序,我正在使用改造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数组。

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

提前谢谢。

3 个答案:

答案 0 :(得分:1)

所以我认为你的问题就在于你正在做这个

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);
    }
});

答案 1 :(得分:0)

您应该提供包含错误的堆栈跟踪。我可以通过浏览代码看到的一件事是,SignUpRequestParent构造函数接受一个SignUpRequestChild对象列表,而您在进行api调用时会传递一个新的SignUpRequestChild对象。

答案 2 :(得分:0)

我认为你是以错误的方式称呼它,试试这个

在RetrofitClient类中添加它

  public static APIService getService() {
    return getClient().create(APIService.class);
  }

然后从任何地方调用它

RetrofitClient.getService.someMethodHere().enqueue ...