2017-12-25 53 views
0

私はこのようなJSONファイルがあります:私はレトロフィットライブラリを使用していると私がすることを示しているインターセプタレベルBODYと応答をチェックしたレトロフィット2 - レスポンスボディはnullを返します(応答コードが200である)

{ 
    "response":{ 
     "ApplicationList":[ 
     { 
      "Id":1, 
      "Name":"SomeApp" 
     } 
     ], 
     "Token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1laWQiOiIyMyIsIm5iZiI6MTUxNDIwMDkyMiwiZXhwIjoxNTQ1NzM2OTIyLCJpYXQiOjE1MTQyMDA5MjJ9.9nyaBfbxPlg8T8WkbBBi34II9NZMtyRpeEJ1s1XCJlo" 
    }, 
    "errorMessageId":0, 
    "errorMessage":null 
} 

を応答は良い(上記のように)。

私はhttp://www.jsonschema2pojo.org/を使用してモデルを作成しましたが、何らかの理由で、Javaモデルへの解析が想定されていません。私はモデルの属性のすべてのnull値を取得しています。

public class SignInUsersResponse implements Parcelable{ 

public Response response; 
public Integer errorMessageId; 
public String errorMessage; 

@Override 
public String toString() { 
    return "SignInUsersResponse{" + 
      "response=" + response + 
      ", errorMessageId=" + errorMessageId + 
      ", errorMessage='" + errorMessage + '\'' + 
      '}'; 
} 

@Override 
public int describeContents() { 
    return 0; 
} 

@Override 
public void writeToParcel(Parcel dest, int flags) { 
    dest.writeParcelable(this.response, flags); 
    dest.writeValue(this.errorMessageId); 
    dest.writeString(this.errorMessage); 
} 

public SignInUsersResponse() { 
} 

protected SignInUsersResponse(Parcel in) { 
    this.response = in.readParcelable(Response.class.getClassLoader()); 
    this.errorMessageId = (Integer) in.readValue(Integer.class.getClassLoader()); 
    this.errorMessage = in.readString(); 
} 

public static final Creator<SignInUsersResponse> CREATOR = new Creator<SignInUsersResponse>() { 
    @Override 
    public SignInUsersResponse createFromParcel(Parcel source) { 
     return new SignInUsersResponse(source); 
    } 

    @Override 
    public SignInUsersResponse[] newArray(int size) { 
     return new SignInUsersResponse[size]; 
    } 
}; 
} 


public class Response implements Parcelable{ 

public List<ApplicationList> applicationList = null; 
public String token; 


@Override 
public String toString() { 
    return "Response{" + 
      "applicationList=" + applicationList + 
      ", token='" + token + '\'' + 
      '}'; 
} 


@Override 
public int describeContents() { 
    return 0; 
} 

@Override 
public void writeToParcel(Parcel dest, int flags) { 
    dest.writeTypedList(this.applicationList); 
    dest.writeString(this.token); 
} 

public Response() { 
} 

protected Response(Parcel in) { 
    this.applicationList = in.createTypedArrayList(ApplicationList.CREATOR); 
    this.token = in.readString(); 
} 

public static final Creator<Response> CREATOR = new Creator<Response>() { 
    @Override 
    public Response createFromParcel(Parcel source) { 
     return new Response(source); 
    } 

    @Override 
    public Response[] newArray(int size) { 
     return new Response[size]; 
    } 
}; 
} 


public class ApplicationList implements Parcelable{ 

public Integer id; 
public String name; 



@Override 
public String toString() { 
    return "ApplicationList{" + 
      "id=" + id + 
      ", name='" + name + '\'' + 
      '}'; 
} 


@Override 
public int describeContents() { 
    return 0; 
} 

@Override 
public void writeToParcel(Parcel dest, int flags) { 
    dest.writeValue(this.id); 
    dest.writeString(this.name); 
} 

public ApplicationList() { 
} 

protected ApplicationList(Parcel in) { 
    this.id = (Integer) in.readValue(Integer.class.getClassLoader()); 
    this.name = in.readString(); 
} 

public static final Creator<ApplicationList> CREATOR = new Creator<ApplicationList>() { 
    @Override 
    public ApplicationList createFromParcel(Parcel source) { 
     return new ApplicationList(source); 
    } 

    @Override 
    public ApplicationList[] newArray(int size) { 
     return new ApplicationList[size]; 
    } 
}; 
} 

すべての3つの異なるクラスファイルにあります。ここでは

は私のモデルクラスです。

そして、これは私が、サーバーを呼び出す方法です:

RHDRService rhdrService = ApiUtilsUser.rhdrService(Constants.RHUSER_BASE_URL); 
    Call<SignInUsersResponse> call = rhdrService.signInUser(signInUserPost); 

    call.enqueue(new Callback<SignInUsersResponse>() { 
     @Override 
     public void onResponse(Call<SignInUsersResponse> call, Response<SignInUsersResponse> response) { 
      Log.d("test", response.body().toString()); 
     } 

     @Override 
     public void onFailure(Call<SignInUsersResponse> call, Throwable t) { 

     } 
    }); 

これは、ログに記録されているものです。

D /テスト:SignInUsersResponse {応答=レスポンス{applicationList = nullを、トークン= 'ヌル'}、errorMessageId = 0、にErrorMessage =' nullを '}

これは、レトロフィットコールに使用されている他のファイルが宣言されている方法です。

public class ApiUtilsUser { 

    public static RHDRService rhdrService(String base_url) { 
     return RetroFitClientUser.getClient(base_url).create(RHDRService.class); 
    } 
} 


public class RetroFitClientUser { 

    private static Retrofit retrofit = null; 

    public static Retrofit getClient(String baseUrl) { 

     HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); 
     // set your desired log level 
     logging.setLevel(HttpLoggingInterceptor.Level.BODY); 
     OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); 
     // add your other interceptors … 
     // add logging as last interceptor 
     httpClient.addInterceptor(logging); // <-- this is the important line! 

     retrofit = new Retrofit.Builder() 
       .baseUrl(baseUrl) 
       .addConverterFactory(GsonConverterFactory.create()) 
       .client(httpClient.build())    // Ako nećemo logovanje onda ova linija ne treba 
       .build(); 

     return retrofit; 
    } 
} 




public interface RHDRService { 

    @Headers("Content-Type: application/json") 
    @POST("api/user/signin") 
    Call<SignInUsersResponse> signInUser (@Body SignInUserPost signInUserPost); 
} 
+0

APIサービス「OkHttpClient」と「Gson」の作成方法を教えてください。 – kernelmaster

+1

Retrofit宣言と呼び出しを追加しました。今まで私はこのスキーマを使用することで何の問題もありませんでした。私はそれに応じて私が受け取るjsonファイルに従って私のモデルに問題があると思う。 – nmirkov

+0

問題はあなたのモデルにあるようです。実際に 'GsonConverterFactory'を追加したかったのです。 'List applicationList'の前に' @SerializedName( "ApplicationList") 'をモデルに追加します。 – kernelmaster

答えて

0

JSONとPOJOは異なります。

デフォルトでは、GsonはFieldNamingPolicy.IDENTITY as field naming policyを使用します。このフィールド命名ポリシーは、JSONをPOJOにマッピングするために使用され、大文字と小文字が区別されます。

異なるフィールド命名規則を使用するか、@SerializedNameを使用して、指定された名前でフィールドを逆シリアル化する必要があることを示すことができます。

class MyPojo { 
    @SerializedName("ApplicationList") 
    List<Application> applicationList; 
} 
+1

ありがとうございました!私は大文字の名前を逃した:( – nmirkov

関連する問題