android - 获取带有改造的网站的 html - Android?

标签 android retrofit retrofit2 androidhttpclient

如何使用 retrofit 获取网站的 html?

例如我有这个url我需要获取此 url 的 html 以及如何加载更多内容。

下面是我的代码:

主要 Activity .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();
}

让我看看:

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

但我需要查看所有 html

最佳答案

解决了我的问题:

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);
    }
}

关于android - 获取带有改造的网站的 html - Android?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44280410/

相关文章:

android - 使用 Retrofit 在 JSON 中上传多部分图像数据?

android - 仅使用缓存响应。无网络通话

android - 尝试获取 POST 方法 Retrofit 的数据

java - 使用 retrofit 和 rxjava 重复调用 API

android - 使用 Retrofit 获取 api

java - 无法通过 UI 从 AsyncTask 发回数据

Android可用性测试——色盲

java - 如何从非动态接收器调用 MainActivity 中带参数的函数?

android - 改造:向android中的服务器发送POST请求

android - zxing 未在 android 中找到异常