2017-09-15 66 views
3

我在我使用改裝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數組。

請指點我該怎麼做。而我錯了。

預先感謝您。

+0

可否請你用錯誤添加堆棧跟蹤? –

+0

也SignUpResponseModel定義 –

+0

@cgomezmendez - 我無法打電話。編碼時的錯誤是:將第11個參數投射到Java.util.List。或者從List 更改爲SignUpRequestChild的方法'SignUpRequestParent'的第11個參數。 –

回答

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

感謝您的回覆。我認爲你的答案正在工作,但現在我得到了java.net。SocketTimeoutException。 –

+0

你可以試試https://stackoverflow.com/questions/39219094/sockettimeoutexception-in-retrofit –

+0

感謝兄弟。它的工作現在。 –

0

您應該提供帶有錯誤的堆棧跟蹤。通過瀏覽代碼我可以看到的一件事是SignUpRequestParent構造函數接受SignUpRequestChild對象的列表,而您在進行api調用時傳遞一個新的SignUpRequestChild對象。

0

我想你調用了錯誤的方式,試試這個

在RetrofitClient類中添加這

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

然後將它從任何地方

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