1

I am using simplxmlconvertor with retrofit in my app to parse xml response from API. Success response from API is given below.

<Success>
  <id>sf98hjf</id>
</Success>

And error response is given below.

<Error>
  <message>No data found</message>
</Error>

My model class to parse success response is given below

 @Root(name = "Success")
public class ResponseModel {
@Element
private String id;

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

}

This works fine when I get success response. But I want to parse error response as well to show error message to user. How can I achieve the same. Now I am getting exception since the structure doesn't match. Thanks in advance.

andro-girl
  • 7,989
  • 22
  • 71
  • 94
  • I'm not sure how you can do it with xml but with GSON you can define custom serializers. I guess you can do it with simplexmlconverter as well. I would use http code response to understand if you need a standard response serializer (code 200) or a error response serializer (code >=400) but it depends on the implementation of your api – Nicola Gallazzi Jan 09 '19 at 13:55
  • 1
    @NicolaGallazzi I have seen Response Interceptors for Retrofit. But my issue is I will be getting status code as 200 even though the response is error. So i cannot differentiate using that. – andro-girl Jan 09 '19 at 14:06
  • Can't you get the response as String and use instanceof operator to detect if it's a "standard" response or an error? – Nicola Gallazzi Jan 09 '19 at 14:15
  • Then I will have to do manual parsing right? – andro-girl Jan 10 '19 at 07:29
  • From Simple documentation, I see that you can use serializer.read method to parse your string source on your Java POJO class http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php#start. Take a look to "Deserializing a simple object". Let me know if this helps, in that case maybe we can think to write down an organized response – Nicola Gallazzi Jan 10 '19 at 08:37

1 Answers1

1

Finally I found one solution for my question. Instead of trying to parse with 2 different models, I intercepted the response and added a common root element. Then made Success and Error nodes as optional. So whether the response is success or Error the same model will parse it. Code is given below.

public class XmlInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Response response = chain.proceed(chain.request());
        ResponseBody body = response.body();
        String wrappedBody = "<Response>" + body.string() + "</Response>";
        return response.newBuilder()
                .body(ResponseBody.create(body.contentType(), wrappedBody))
                .build();
    }
}

Model class to parse response.

@Root(name = "Response")
public class UploadResponseModel {
@Element(required = false)
private UploadSuccessModel Success;
@Element(required = false)
private ErrorModel Error;

public UploadSuccessModel getSuccess() {
    return Success;
}

public void setSuccess(UploadSuccessModel success) {
    Success = success;
}

public ErrorModel getError() {
    return Error;
}

public void setError(ErrorModel error) {
    Error = error;
}
}
andro-girl
  • 7,989
  • 22
  • 71
  • 94