In this blog, we will learn how to handle the HTML Response from HTTP Request using Retrofit.
While using APIs from your server you are definitely sure about the response you get, but sometimes you need to go to some other servers and access the data from there and you cannot change their response.
One approach is to get your server developer fetch the data from the server you intend to fetch data and then revert to you in the form you want. But this will create a time-consuming process.
I just needed to fetch some data from a public URL, but ht e response was only in HTML and was for web pages. So one approach was to display the data in a web view, but in this, I don’t get to access the data.
So in order to access the data and that too without the model class from retrofit was what I needed and here in this blog, I will show you how?
Before discussing further, I would like to add that since the response is in Html. You would need some kind of library that can parse the HTML response received so that you can store your desired information.
If you think you don’t need a parser, you can definitely move ahead to APPROACH section
I have used the Jsoup Html Parser so that I can create a document from the response received and access the data easily.
For using this library, you need to add this dependency in your module level Gradle file.
1 |
compile 'org.jsoup:jsoup:1.10.2' |
APPROACH :
- We don’t know how the response would be, so we need to create a retrofit Object accordingly without the model class.
- For this, we will use the ResponseBody Object provided by the Retrofit only.
- When a response is received in the callback, we will extract the body of the response and convert it to a string by calling the string() method.
- Now we will Create a document from this string by passing it to Jsoup.parse()method
- Now you can easily traverse the document created and extract the data you need.
CODE :
Creating a Retrofit Call :
1 2 3 4 5 |
Call<ResponseBody> myHtmlCall = RetrofitClient.getClient().create(ApiInterface.class).getHtmlResponse( "YOUR_URL_TO_CONNECT" ); myHtmlCall.enqueue(myHTMLResponseCallback); |
CallBack for the Call :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
private Callback<ResponseBody> myHTMLResponseCallback = new Callback<ResponseBody>() { @Override public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { try{ String html = response.body().string(); Document document = Jsoup.parse(html); Elements elements = document.select("name_of_tag_you want_to_get"); for (Element element:elements) { if (element.attr("name_of_attribute_you want to check").equals("value_of_the_attribute")){ //Save As you want to Log.d(TAG, " myHTMLResponseCallback : " +element.attr("value")); } } }catch (Exception e){ e.printStackTrace(); } } @Override public void onFailure(Call<ResponseBody> call, Throwable t) { t.printStackTrace(); } }; |
If you are wondering what retrofit call is actually doing or facing some difficulties in understanding the retrofit call you can read it in the article shared below.