OkHTTP页面源代码

时间:2015-10-10 17:08:36

标签: android okhttp

我正在尝试使用html库获取OkHTTP页面源代码。但是以下代码不返回页面的代码。请帮助我获取网页代码。

private void loadUrl() {
        OkHttpClient client = new OkHttpClient();
        try {
            post(client, currentUrl, new Callback() {
                @Override
                public void onFailure(Request request, IOException e) {

                }

                @Override
                public void onResponse(Response response) throws IOException {
                    if (response.isSuccessful()) {
                        String responseStr = response.body().string();
                        // Do what you want to do with the response.
                    } else {
                        // Request not successful
                    }
                }
            }).;
        } catch (IOException e) {

        }
    }

    private Call post(OkHttpClient client, String url, Callback callback) throws IOException{
        Request request = new Request.Builder()
                .url(url)
                .removeHeader("User-Agent")
                .addHeader("User-Agent", userAgent)
                .build();

        Call call = client.newCall(request);
        call.enqueue(callback);
        return call;
    }

1 个答案:

答案 0 :(得分:0)

我刚才在这里回答了同样的问题Save .html file into internal storage using OkHttp lib。你的代码应该可行。但是,我刚发现OkHttp返回的URL错误,最后有#这样的OkHTTP page source