首页 > 解决方案 > 改造不返回完整的响应体

问题描述

我正在使用 Retrofit 从 Url 获取 html 页面源。但是 Retrofit 得到了 html 页面源代码的一小部分

我试图将 ResponseBody 更改为 String 但没有奏效。

private static final String TAG = "TESTTESTTESTTEST";
    private static String url = "https://www.hepsiburada.com/lassa-235-65r17-108h-xl-competush-l-p-OTLST216410?magaza=LastikArt%C4%B1&utm_source=pc&utm_medium=cimri&utm_campaign=c&utm_content=c&utm_term=5083&wt_pc=cimri.c.5083.pc/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://www.google.com/")
                .client(getOkHttpClient())
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();

        IGetHtml iGetHtml = retrofit.create(IGetHtml.class);
        Call<ResponseBody> stringCall = iGetHtml.getHtml(url);
        stringCall.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (response.isSuccessful()) {
                    try {
                        Log.e(TAG, "onResponse: " + response.body().string());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.e(TAG, "onFailure: " + t);
            }
        });


    }

    private OkHttpClient getOkHttpClient() {
        OkHttpClient client = new OkHttpClient();
        try {
            client = new OkHttpClient.Builder()
                    .sslSocketFactory(new TLSSocketFactory())
                    .build();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        return client;
    }

我期望完整的页面源,但它返回页面源的一小部分(它返回大约 4271 个字符)。

标签: androidretrofitretrofit2okhttpokhttp3

解决方案


推荐阅读