Handle the HTML Response from HTTP Request using Retrofit


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. We don’t know how the response would be, so we need to create a retrofit Object accordingly without the model class.
  2. For this, we will use the ResponseBody Object provided by the Retrofit only.
  3. 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.
  4. Now we will Create a document from this string by passing it to Jsoup.parse()method
  5. Now you can easily traverse the document created and extract the data you need.


Creating a Retrofit Call :

CallBack for the Call :

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.

Using Retrofit library to make Http Requests

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


  • Morten Slott Hansen
    I’d recommend using

    Jsoup.parse(html, “”, Parser.xmlParser());

    to make it more flexible when handling poorly written HTML

    • anchit (Moderator)
      thanks for the information
      • Arshavin
        And why not use the jsoup library itself to fetch html from network? I mean what is the advantage of using retrofit in this instance?
        • anchit (Moderator)

          If you can use jsoup then that is wonderful. But the problem that this blog is for the users that are stuck to retrofit only and hence they can also work. Also, people usualy use retrofit only to make public API calls and in some error cases you get a return type in html and in success cases you get response as json so if you know this approach then you can handle both json and html with retrofit only. Hope you do get the answers of your concerns.

  • css.php

    Webkul is a truly trusted and supported IT Company to develop business ideas for e-commerce. They provide professional support after deploying the solution to production and be responsible to act for fixing the reported issues or system errors. We highly encourage to deal with Webkul for business development.

    Hussein Zawia

    Hussein Zawia

    Co-founder, 24Dokan

    Talk to Sales


    Live Chat
    Start a Project

      Message Sent!

      If you have more details or questions, you can reply to the received confirmation email.

      Back to Home