Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用webview或Web浏览器在android Studio中显示OKHTTP html响应并与之交互_Javascript_Android_Html_Android Webview_Okhttp3 - Fatal编程技术网

Javascript 如何使用webview或Web浏览器在android Studio中显示OKHTTP html响应并与之交互

Javascript 如何使用webview或Web浏览器在android Studio中显示OKHTTP html响应并与之交互,javascript,android,html,android-webview,okhttp3,Javascript,Android,Html,Android Webview,Okhttp3,我正在构建一个android应用程序。我使用OKHTTP构建了一个请求,得到的响应是由html css和js内容组成的字符串。这个响应实际上是一个表单,用户必须使用它来允许应用程序与给定的网站进行通信 现在,我希望用户能够以html页面的形式看到该响应,并单击按钮以允许通信。唯一的问题是我不知道如何在webview或web浏览器中将该响应显示为html 从主要活动: Authenticate myAouth = new Authenticate

我正在构建一个android应用程序。我使用OKHTTP构建了一个请求,得到的响应是由html css和js内容组成的字符串。这个响应实际上是一个表单,用户必须使用它来允许应用程序与给定的网站进行通信

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

从主要活动:

                          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;
}}
任何帮助都是必要的


您可以使用以下代码将字符串转换为HTML,然后在Web视图中显示

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

你好,穆罕默德,谢谢你的回复。我试过了,但它说stringhtml=newstring(响应,“UTF-8”);这是多余的。是否有其他方法将字符串转换为UTF-8。“我的网络视图”上的更多内容不显示任何内容,全部为空。请提供任何帮助。当
response
为HTML代码时,此代码适用于我。我还没有检查OKHTTP中响应的外观,但是您可以尝试
stringHTML=newstring(response.body().String(),“UTF-8”)你好,穆罕默德,是的,它确实有效。我意识到webview不能在android emulator上运行,我必须使用真正的设备才能运行,谢谢