3

I am developing an application in i am using retrofit 2. My request JSON is as below

{
    "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" 
    }]
}

My API service call is as below

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

My API Util class is as below

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

My Retrofit client class is as follows:

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

My request model classes are as follows:

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

Below is the calling code of web service where i am getting error.

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

The first line of calling code is giving the error. As i do not know how to create the nested JSON array from classes.

Please goude me what to do. And where i am wrong.

Thank you in advance.

Manoj Fegde
  • 4,786
  • 15
  • 50
  • 95

3 Answers3

1

So I think your problem is this you are doing this

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

but your

new SignUpRequestChild(Education,Sports,Hobby))

it's expected to be a list as per your constructor so you should really be doing this

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);
    }
});
Chris Gomez
  • 6,644
  • 4
  • 18
  • 39
0

You should provide a stack trace with the error. One thing I can see from skimming the code is that the SignUpRequestParent constructor accepts a list of SignUpRequestChild objects whereas you're passing a new SignUpRequestChild object when making the api call.

0

I think you are calling it the wrong way, try this

Add this in your RetrofitClient Class

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

Then to call it from anywhere

RetrofitClient.getService.someMethodHere().enqueue ... 
Veeresh Charantimath
  • 4,641
  • 5
  • 27
  • 36