改造没有解析JSONObject

use*_*977 0 parsing android jsonobject retrofit

成功登录会JSONObject从服务器返回以下内容:

{"success":true,"message":"Sign in success.","response_data":{"user_id":"24", "email_id":"user@gmail.com", "secret_code": "You did it!"}}
Run Code Online (Sandbox Code Playgroud)

我想把response_data信息放到我的User对象中.我以前做过这样的事情:

String getResponse = jsonObject.getString("response_data");

Gson gson = new GsonBuilder()
        .disableHtmlEscaping()
        .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
        .setPrettyPrinting()
        .serializeNulls()
        .create();

//All the data in the `response_data` is initialized in `User`
User user = gson.fromJson(getResponse, User.class);
Run Code Online (Sandbox Code Playgroud)

现在我尝试在改造中做同样的事情:

初始化RestAdapter +接口:

public class ApiClient {
    private static RetrofitService sRetrofitService;

    public static RetrofitService getRetrofitApiClient() {
        if (sRetrofitService == null) {
            RestAdapter restAdapter = new RestAdapter.Builder()
                    .setLogLevel(RestAdapter.LogLevel.FULL)
                    .setEndpoint("http://xxx.xxx.xxx.xxx/")
                    .build();

            sRetrofitService = restAdapter.create(RetrofitService.class);
        }

        return sRetrofitService;
    }

    public interface RetrofitService {

        @FormUrlEncoded
        @POST("/login")
        public void login(@Field("email_id") String emailId, @Field ("password") String password, 
                          Callback <User> callback);                                                                    
    }
}
Run Code Online (Sandbox Code Playgroud)

主要活动:

  ApiClient.getRetrofitApiClient().login(email.getText().toString(), password.getText().toString(),       
      new Callback<User>() {
            @Override
            public void success(User user, Response response) {
                User user1 = user; //null
                Toast.makeText(this, "user is: "+user1.toString(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void failure(RetrofitError error) {
                Toast.makeText(this, "Failed Login", Toast.LENGTH_SHORT).show();
            }
        });
Run Code Online (Sandbox Code Playgroud)

用户:

public class User {

    private String userId;
    private String emailId;
    private String code;

    public User() {

    }

    ... getters
    ... setters
}
Run Code Online (Sandbox Code Playgroud)

工作中的Retrofit代码MainActivity和我得到的回应log:

{"success":true,"message":"Sign in success.","response_data":{"user_id":"24", "email_id":"user@gmail.com", "secret_code": "You did it!"}}
Run Code Online (Sandbox Code Playgroud)

但是它没有解析response_data我的User对象.

我该如何解决?

cyr*_*xis 5

您需要创建一个响应对象

public class UserResponse {
  private User responseData;
  private String message;
  private boolean success;
}
Run Code Online (Sandbox Code Playgroud)

并更改您的回调以返回UserResponse

public interface RetrofitService {
    @FormUrlEncoded
    @POST("/login")
    public void login(@Field("email_id") String emailId, @Field ("password") String password, 
                      Callback <UserResponse> callback);                                                                    
}
Run Code Online (Sandbox Code Playgroud)

注意:您还需要创建自定义Gson实例,并将其应用于其余适配器.

RestAdapter restAdapter = RestAdapter.Builder()
  .setEndpoint("xxx")
  .setConverter(new GsonConverter(gson))
  .build()
Run Code Online (Sandbox Code Playgroud)