0

I am building an Android app that should read the server response and process the data.

As user I am able to control whether I get XML or Json type of response. However, in certain cases, it is possible that the data will not be in either XML or Json format but raw HTML and this is where I am struggling.

At this moment I am working with XML formatted response. Right now I am using HttpUrlConnection where I get the response as String. Then, I use <?xml version="1.0" encoding="utf-8"?> and look for it in the response string. If it is found, then I know that the request was successful and I got the response that I needed so I proceed to parse and work with data and if It was not found then I just display an error and that's it.

Now, I am trying to move to Retrofit 2 with its converters and I have successfully implemented it with SimpleXML and I am able to parse the XML data that I am receiving, but as soon as I get HTML data, the app crashes because it is not able to parse HTML with XML parser, which is understandable.

So my questions are:

  1. Is there a way to deal with this?
  2. Would it be possible to get String response with Retrofit and if so, would there be any advantage to going through the same process but with Retrofit instead of HttpUrlConnection?
Vitaliy-T
  • 733
  • 6
  • 23

1 Answers1

0

There are a couple of things you could try :-

  • Ask retrofit to return String response as is and handle it manually

  • Ask retrofit to return Object and perform runtime instanceof checks to determine your next course of action.

You might also want to look into this.

Nilesh PS
  • 356
  • 3
  • 8