【问题标题】:How to display and interact with OKHTTP html response in android Studio using webview or Web browser如何使用 webview 或 Web 浏览器在 android Studio 中显示 OKHTTP html 响应并与之交互
【发布时间】:2017-10-11 14:33:28
【问题描述】:

我正在构建一个安卓应用程序。我已经使用 OKHTTP 构建了一个请求,并将响应作为由 html css 和 js 内容组成的字符串。此响应实际上是用户必须使用的表单,以允许应用程序与给定网站进行通信。

现在我希望用户能够将该响应视为 html 页面并单击一个按钮以允许通信。唯一的问题是我不知道如何在 webview 或 web 浏览器中将该响应显示为 html。

来自 MainActivity:

                          Authenticate myAouth = new Authenticate("myCostumerKey","mySecretKey");
                           try {
                               myResponse=myAouth.run("myUrlHere");
                               //System.out.println( myResponse);
                           } catch (Exception e) {
                               e.printStackTrace();
                           }

认证类

public class Authenticate {
private final OkHttpClient client;
String[] myResponse =new String[2];
public Authenticate( final String consumerKey, final String consumerSecret) {

    client = new OkHttpClient.Builder()
            .authenticator(new Authenticator() {
                @Override public Request authenticate(Route route, Response response) throws IOException {
                    if (response.request().header("Authorization") != null) {
                        return null; // Give up, we've already attempted to authenticate.
                    }

                    System.out.println("Authenticating for response: " + response);
                    System.out.println("Challenges: " + response.challenges());
                    String credential = Credentials.basic(consumerKey, consumerSecret);
                    Request myRequest =response.request().newBuilder()
                            .header("Authorization", credential)
                            .build();
                    HttpUrl myURL = myRequest.url();
                    myResponse[0]= String.valueOf(myURL);
                    return myRequest;
                }
            })
            .build();
}

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public String[] run(String url) throws Exception {
    Request request = new Request.Builder()
            .url(url)
            .build();


    try (Response response = client.newCall(request).execute()) {
        if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
        myResponse[1]=response.body().string();
        System.out.println(" URL is "+myResponse[0]+" my response body is "+myResponse[1]);

    }

    return myResponse;
}}

如有任何帮助,将不胜感激。

亲切的问候

【问题讨论】:

    标签: javascript android html android-webview okhttp3


    【解决方案1】:

    您可以使用以下代码将String转换为HTML,然后将其显示在WebView中

        try {
            String html = new String(response, "UTF-8");
            String mime = "text/html";
            String encoding = "utf-8";
    
            myWebView.getSettings().setJavaScriptEnabled(true);
            myWebView.loadDataWithBaseURL(null, html, mime, encoding, null);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
    

    【讨论】:

    • 您好 Mohamed,感谢您的回复。我试过了,但它说 String html = new String(response, "UTF-8");是redudante。是否有另一种将字符串转换为 UTF-8 的方法。更多在我的 webview 显示什么都没有任何帮助请。
    • response 是 HTML 代码时,此代码对我有用。我还没有检查 OKHTTP 中的响应是什么样的,但你可以试试String html = new String(response.body().string(), "UTF-8");
    • 嗨 Mohamed 是的,它确实有效。我意识到 webview 在 android 模拟器上不起作用我必须使用真实设备才能使其工作谢谢
    猜你喜欢
    • 2013-08-27
    • 2011-06-05
    • 2014-06-15
    • 2011-09-02
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 2019-08-01
    相关资源
    最近更新 更多