Get html of a website with retrofit - Android?
Asked Answered
K

2

9

How can I get html of a website with retrofit ?

for example I have this url and I need to get html of this url and how can I load more .

Bellow is my code :

MainActivity.java:

public class MainActivity extends AppCompatActivity {
    TextView txt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txt = (TextView) findViewById(R.id.txt);

        OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
                .build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://www.instagram.com/elde0596/")
                .addConverterFactory(GsonConverterFactory.create())
                .client(okHttpClient)
                .build();

        final Interface_Web request = retrofit.create(Interface_Web.class);
        Call<ResponseBody> call = request.getHtml();
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                txt.setText(response.body().source().toString());
                Log.i("SDADASDAWEQ", "A " + response.body().toString());
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.i("SDADASDAWEQ", "B " + t.getMessage());
            }
        });

    }
}

Interface_Web.java :

public interface Interface_Web {
    @GET("/")
    Call<ResponseBody> getHtml();
}

Just show me :

[size=9500 text=<!DOCTYPE html>\n<!--[if lt IE 7]>      <html lang="en" class="no…]

But I need to see all of html .

Kunzite answered 31/5, 2017 at 9:9 Comment(3)
please mention what have you tried and where your are lacking ,and please put some code hereWilliawilliam
i want all html code, but here its asking for tag name to get code snippet. here is my code--> 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")){ } }Paulownia
Thanks it is helpful for me. And let me know how to print whole content of html, As per your solution its only printing one line not whole html content. Please let me know.Thanks!!Spurious
K
4

Resolved my problem :

public class SecondClass extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.class_second);
        Dispatcher dispatcher = new Dispatcher(Executors.newFixedThreadPool(20));
        dispatcher.setMaxRequests(20);
        dispatcher.setMaxRequestsPerHost(1);

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .dispatcher(dispatcher)
                .connectionPool(new ConnectionPool(100, 30, TimeUnit.SECONDS))
                .build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(HttpUrl.parse("https://www.x.x/x/"))
                .addConverterFactory(PageAdapter.FACTORY)
                .build();

        PageService requestAddress = retrofit.create(PageService.class);
        Call<Page> pageCall = requestAddress.get(HttpUrl.parse("https://www.x.x/x/"));
        pageCall.enqueue(new Callback<Page>() {
            @Override
            public void onResponse(Call<Page> call, Response<Page> response) {
                Log.i("ADASDASDASD", response.body().content);
            }
            @Override
            public void onFailure(Call<Page> call, Throwable t) {

            }
        });
    }

    static class Page {
        String content;

        Page(String content) {
            this.content = content;
        }
    }

    static final class PageAdapter implements Converter<ResponseBody, SecondClass.Page> {
        static final Converter.Factory FACTORY = new Converter.Factory() {
            @Override
            public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
                if (type == SecondClass.Page.class) return new SecondClass.PageAdapter();
                return null;
            }
        };

        @Override
        public SecondClass.Page convert(ResponseBody responseBody) throws IOException {
            Document document = Jsoup.parse(responseBody.string());
            Element value = document.select("script").get(1);
            String content = value.html();
            return new SecondClass.Page(content);
        }
    }

    interface PageService {
        @GET
        Call<SecondClass.Page> get(@Url HttpUrl url);
    }
}
Kunzite answered 2/8, 2017 at 12:44 Comment(0)
F
3

I think this would help you. If you create your call object as ResponseBody you can get html like this:

Call<ResponseBody> call = Interface_Web.getJSONSignIn(...)
call.enqueue(new Callback<ResponseBody>() {
    @Override
    public void onResponse(Response<ResponseBody> response, Retrofit retrofit) {
        // access response code with response.code()
        // access string of the response with response.body().string()
    }

    @Override
    public void onFailure(Throwable t) {
        t.printStackTrace();
    }
});
Fridafriday answered 31/5, 2017 at 9:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.