Get html of a website with retrofit - Android?

后端 未结 2 819
你的背包
你的背包 2021-01-02 11:42

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

2条回答
  •  庸人自扰
    2021-01-02 12:13

    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 pageCall = requestAddress.get(HttpUrl.parse("https://www.x.x/x/"));
            pageCall.enqueue(new Callback() {
                @Override
                public void onResponse(Call call, Response response) {
                    Log.i("ADASDASDASD", response.body().content);
                }
                @Override
                public void onFailure(Call call, Throwable t) {
    
                }
            });
        }
    
        static class Page {
            String content;
    
            Page(String content) {
                this.content = content;
            }
        }
    
        static final class PageAdapter implements Converter {
            static final Converter.Factory FACTORY = new Converter.Factory() {
                @Override
                public Converter 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 get(@Url HttpUrl url);
        }
    }
    

提交回复
热议问题