OkHttpClient不适用于“ http”网址(“ http://www.example.com”),但适用于“ https”网址(“ https://www.example.com”)

时间:2020-02-16 13:56:08

标签: android retrofit okhttp android-networking

此代码运行onFailure函数。

OkHttpClient client;
client = new OkHttpClient();

此处的网址是 http 网址。

Request request = new Request.Builder().url("http://www.example.com").build();
client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
                textView.setText("Failed to get response");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response.isSuccessful()){
                    final String myResponse = response.body().string();
                    MainActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            textView.setText(myResponse);
                        }
                    });
                }
            }
        });

但是当网址是 https 网址时 Request request = new Request.Builder().url("https://www.example.com").build(); 然后调用onResponse

如何使此功能适用于http个网站。我需要它处理的网站是仅http的网站。

0 个答案:

没有答案