简体   繁体   English

如何使用AndroidAnnotations解析对POJO的JSON响应?

[英]How to parse JSON Response to POJO with AndroidAnnotations?

I'm using AndroidAnnotations to build a Rest for an Android Application. 我正在使用AndroidAnnotations为Android应用程序构建Rest。 On the Serverside im using PHP, which send a json looking like : 在使用PHP的服务器端即时消息上,它发送一个json,如下所示:

{"tag":"register","success":0,"error":2,"msg":"User already existed","body":[]}

I have two POJOS : 我有两个POJOS:

User.java: User.java:

public class User implements Serializable {
    private String name;
private String email;
private String password;

    //getter and setter Methods
}

Response.java: Response.java:

public class RegistrationResponse implements Serializable {

private String tag;
private int success;
private int error;
private String msg;
private String body;

    //getter and setter Methods
}

Rest Client: 休息客户:

@Rest(rootUrl = "http://my.domain.com", converters = {
    MappingJacksonHttpMessageConverter.class,
    StringHttpMessageConverter.class, GsonHttpMessageConverter.class }, interceptors = { MyInterceptor.class })
public interface RestClient extends RestClientErrorHandling {
    @Post("/user/register/{name}/{email}/{pass}")
    @Accept(MediaType.APPLICATION_JSON_VALUE)
    Response sendUserRegistration(User user, String name, String email,
        String pass);

    RestTemplate getRestTemplate();
}

Activity.java: Activity.java:

//User and Response are POJOs
Response result = RestClient.sendUserRegistration(user,
                user.getName(),user.getEmail(),user.getPassword());

But i got an Null Pointer Exception error on Activity.java. 但是我在Activity.java上遇到了空指针异常错误 But if i change the return value of "sendUserRegistration" function to String all work. 但是,如果我将“ sendUserRegistration”函数的返回值更改为String,则所有工作正常。 So my "Response" POJO seems not to be converted from AndroidAnnotations. 因此,我的“响应” POJO似乎没有从AndroidAnnotations转换而来。

How can i convert the Rest Response to my "Response"-POJO using AndroidAnnotations? 如何使用AndroidAnnotations将“休息响应转换为“响应” -POJO?

AA relies on Spring Android RestTemplate to make the rest call. AA依靠Spring Android RestTemplate进行rest调用。 And in order to build requests and handle responses this lib uses converters. 为了建立请求和处理响应,此库使用转换器。 And to know which converter the RestTemplate should use, it checks the content-type response header. 为了知道RestTemplate应该使用哪个转换器,它会检查content-type响应头。 As MappingJacksonHttpMessageConverter and GsonHttpMessageConverter handles only http response with content-type=application/json and your result is converted to string, I'm pretty sure you forgot to set this header in your php server. 由于MappingJacksonHttpMessageConverterGsonHttpMessageConverter仅处理content-type=application/json http响应,并且您的结果转换为字符串,因此我很确定您忘记了在PHP服务器中设置此标头。 So it send the default one (ie: text/plain ) which is only handle by StringHttpMessageConverter . 因此,它将发送仅由StringHttpMessageConverter处理的默认值(即text/plain )。

Also, the body field is an object in your json example, but in your POJO you declared it as a String. 另外, body字段在json示例中是一个对象,但是在POJO中,您将其声明为String。 So parsing will fail on this point. 因此,解析将在这一点上失败。

You don't need to return the entire response object per rest call, just set the response to your custom object. 您无需在每个rest调用中返回整个响应对象,只需将响应设置为您的自定义对象即可。 Or you can also return a JsonObject also and use gson to convert it later on. 或者,您也可以返回JsonObject并稍后使用gson对其进行转换。

@Rest(rootUrl = "http://my.domain.com", converters = {
MappingJacksonHttpMessageConverter.class,
StringHttpMessageConverter.class, GsonHttpMessageConverter.class }, interceptors = {   MyInterceptor.class })
public interface RestClient extends RestClientErrorHandling {

    @Post("/user/register/{name}/{email}/{pass}")
    @Accept(MediaType.APPLICATION_JSON_VALUE)
    User sendUserRegistration(User user, String name, String email,
    String pass);

    RestTemplate getRestTemplate();
}

then just simply call 然后只需打电话

User newUser = RestClient.sendUserRegistration(user,
                user.getName(),user.getEmail(),user.getPassword());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM