bkach
bkach

Reputation: 1441

Retrofit 2.0 Parse HTML

I'm wondering if there's a way to parse HTML with retrofit 2.0 on Android, or if not, if there is a way to simply get a raw response and parse it myself without forcing the result through a convertor factory and thus dealing with parsing issues.

I've tried using the simplexml convertor factory, but most web pages are not nearly as strict as your typical XML API, and the inconsistences result in parsing issues and eventually a crash.

What would be a good approach if looking to scrape a webpage with retrofit?

Upvotes: 1

Views: 4442

Answers (1)

user4774371
user4774371

Reputation:

if there is a way to simply get a raw response

I don't know about parsing HTML but you can get raw response using response.body(). See following code:

        Call<Void> restCall = restClient.getUserService().readUserData(object);
        restCall.enqueue(new Callback<Void>(){
            @Override
            public void onResponse(Response<Void> response) {

                if(response.isSuccess()){
                    Log.d(TAG, "  response from server" + response.body());
                }else{
                    Log.d(TAG, " Some error occurred ");
                }
            }

            @Override
            public void onFailure(Throwable t) {
                Log.d(TAG, " Web service failure, exception ");
            }
        });

Interface:

@Headers("Content-Type: application/json")
@POST("user/readdata")
Call<Void> readUserData(@Body JsonObject jsonObject);

Upvotes: 3

Related Questions