2016-08-16 5 views
1

私はプロジェクトに2を使用していましたが、私はjsonの応答を解析できません。BEGIN_ARRAYが必要ですが、改造2ではBEGIN_OBJECTでしたか?

Retrofit retrofit = new Retrofit.Builder().baseUrl("BASE_URL").addConverterFactory(GsonConverterFactory .create()).build(); 
final RetrofitApi request = retrofit.create(RetrofitApi.class); 
LoginModel loginModel = new LoginModel("[email protected]",[email protected]#"); 
Call<List<LoginResponse>> listCall = request.Loginner(loginModel); 
listCall.enqueue(new Callback<List<LoginResponse>>() { 
     @Override 
     public void onResponse(Call<List<LoginResponse>> call, Response<List<LoginResponse>> response) { 
      List<LoginResponse> loginModelList = new ArrayList<>(); 
      loginModelList = response.body(); 

JSONレスポンスは、JSONのこのタイプのためにゲッターとセッターを書くためにどのように

{ 
     "response": { 
     "errorCode": "", 
     "errorMsg": "", 
     "successCode": "SUB001", 
     "successMsg": "Login successfully", 
     "data": { 
      "user_details": { 
      "salutation": "Mr.", 
      "first_name": "User", 
      "last_name": "R", 
      "email": "[email protected]", 
      "alternative_email": "", 
      "mobile_number": "54312", 
      "phone_number": "", 
      "title_position": "", 
      "department": "", 
      "city": "", 
      "street": "", 
      "state": "", 
      "postcode": "", 
      "display_name": "Bijoy R", 
      "assistant_details": "", 
      "country_name": "India", 
      "institution_name": "KLO", 
      "user_type": "commercial" 
      }, 
      "payment_details": [ 
      { 
       "dateandtime": "28-07-2016 17:4736", 
       "payment_mode": "DD", 
       "items": "Accompanying Person", 
       "transactionId": "", 
       "amount": "3000.00", 
       "status": "Failed" }  ], 
     } 
     } 
    } 

のようでした。 JSON応答形式に関連するモデルクラスを書くための例上記

public class ServerResponse<T> { 

    private ResponseStatus response; 
    private T data; 

    public ResponseStatus getResponse() { 
     return response; 
    } 

    public void setResponse(ResponseStatus response) { 
     this.response = response; 
    } 

    public T getData() { 
     return data; 
    } 

    public void setData(T data) { 
     this.data = data; 
    } 
} 

public class ResponseStatus { 

    private String errorCode; 
    private String errorMsg; 
    private String successCode; 
    private String successMsg; 

    public String getErrorCode() { 
     return errorCode; 
    } 

    public void setErrorCode(String errorCode) { 
     this.errorCode = errorCode; 
    } 

    public String getErrorMsg() { 
     return errorMsg; 
    } 

    public void setErrorMsg(String errorMsg) { 
     this.errorMsg = errorMsg; 
    } 

    public String getSuccessCode() { 
     return successCode; 
    } 

    public void setSuccessCode(String successCode) { 
     this.successCode = successCode; 
    } 

    public String getSuccessMsg() { 
     return successMsg; 
    } 

    public void setSuccessMsg(String successMsg) { 
     this.successMsg = successMsg; 
    } 
} 

public class LoginResponse { 

    @SerializedName("userDetails") 
    private User userDetails; 

    @SerializedName("payment_details") 
    private List<PaymentDetail> paymentDetails; 

    public User getUserDetails() { 
     return userDetails; 
    } 

    public void setUserDetails(User userDetails) { 
     this.userDetails = userDetails; 
    } 

    public List<PaymentDetail> getPaymentDetails() { 
     return paymentDetails; 
    } 

    public void setPaymentDetails(List<PaymentDetail> paymentDetails) { 
     this.paymentDetails = paymentDetails; 
    } 
} 

public class User { 
    /** 
    * Ignore implement 
    */ 
} 

public class PaymentDetail { 
    /** 
    * Ignore implement 
    */ 
} 

使用:レトロフィットAPIインターフェイスの戻り値の型がCall<ServerResponse<LoginResponse>>ある

+0

からCall<LoginResponse>Response<List<LoginResponse>>Call<List<LoginResponse>>を交換してください試してみてください、ショー 'LoginResponse'クラス –

+0

あなたはログからあなたのJSONレスポンスを取得したのですか?あなたのpojoクラスは大丈夫です。 'payment_details'がサーバーからの応答で実際に配列されていることを確認する必要があります。 –

+0

私は応答がうまくいっていますが、それを解析することはできません。改造はそのBEGIN_ARRAYを示していますが、それは.. –

答えて

4

Response<LoginResponse>

+0

です。これは動作し、私のモデルクラスはすべてDTOプラグインを使用して生成されます。ありがとう兄貴! :) –

0

変更は、このコードをfllow。あなたのJSONレスポンスにCamelCaseスタイルを使用し、GSONとの組み合わせを改善することをお勧めします。 すみません、私の英語はとても限られています。

関連する問題