Android WebView.postUrl() 在发布到 HTTPS URL 时显示空白屏幕

发布于 2024-12-08 14:21:50 字数 1353 浏览 0 评论 0原文

在我的 Android 应用程序中,我将数据从 WebView 发布到 https servlet URL,如下所示。

String postData = "fileContents=" + fileCon;
WebView.postUrl(url, EncodingUtils.getBytes(postData, "BASE64"));

上面代码中的 URL 是我必须发布的 servlet URL一些数据,然后我从那里重定向到其他一些 URL。

当 servlet URL 仅为 HTTP 时,上述代码运行良好。但当更改为 HTTPS 时,它显示空白屏幕。

我尝试了以下 Android HTTPS 问题的解决方案: http://blog.antoine.li/index.html php/2010/10/android-trusting-ssl-certificates/

我从 onCreate() 方法中删除了上述代码并尝试了以下代码

ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("fileContents", fileCon));
DefaultHttpClient client = new MyHttpClient(getApplicationContext());
try {   
    HttpPost request = new HttpPost(url);
    UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
    request.setEntity(formEntity);
    HttpResponse resp = client.execute(request);
} catch(Exception e){
    e.printStackTrace();
}

现在我可以发布数据并从那里它也正在重定向。但我仍然看到一个空白屏幕。

是因为我没有 loadUrlpostUrl 我看到的是空白屏幕吗?

或者我应该将上面的代码放在 WebView 的任何方法中?

In my android app, I am posting data to a https servlet URL from a WebView as shown below

String postData = "fileContents=" + fileCon;
WebView.postUrl(url, EncodingUtils.getBytes(postData, "BASE64"));

The URL in the above code is a servlet URL for which I have to post some data and from there I am redirecting to some other URL.

The above code worked fine when the servlet URL is just HTTP. But when changed to HTTPS, it is showing a blank screen.

I tried the following solution for android HTTPS problem:
http://blog.antoine.li/index.php/2010/10/android-trusting-ssl-certificates/

I removed the above code from onCreate() method and tried the following code

ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("fileContents", fileCon));
DefaultHttpClient client = new MyHttpClient(getApplicationContext());
try {   
    HttpPost request = new HttpPost(url);
    UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
    request.setEntity(formEntity);
    HttpResponse resp = client.execute(request);
} catch(Exception e){
    e.printStackTrace();
}

Now I am able to post the data and from there it is redirecting also. But still I am seeing a blank screen.

Is it because I don't have either loadUrl or a postUrl I am seeing a blank screen?

Or should I put the above code in any method of WebView?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

汹涌人海 2024-12-15 14:21:50

试试这个

    ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
    postParameters.add(new BasicNameValuePair("fileContents", fileCon));
    DefaultHttpClient client = new MyHttpClient(getApplicationContext());
try {   
    HttpPost request = new HttpPost(url);
    UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
    request.setEntity(formEntity);
    HttpResponse resp = client.execute(request);

    //Get data
     HttpEntity entity = resp.getEntity();
     InputStream is = entity.getContent();
     String data = convertStreamToString(is);
     browser=(WebView)findViewById(R.id.myWebView);
     browser.loadData(data,"text/html", "UTF-8");
} catch(Exception e){
    e.printStackTrace();
}

从InputStream到String的方法:

private static String convertStreamToString(InputStream is) {

BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();

String line = null;
try {
    while ((line = reader.readLine()) != null) {
        sb.append((line + "\n"));
    }
} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        is.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
return sb.toString();

}

Try this

    ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
    postParameters.add(new BasicNameValuePair("fileContents", fileCon));
    DefaultHttpClient client = new MyHttpClient(getApplicationContext());
try {   
    HttpPost request = new HttpPost(url);
    UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
    request.setEntity(formEntity);
    HttpResponse resp = client.execute(request);

    //Get data
     HttpEntity entity = resp.getEntity();
     InputStream is = entity.getContent();
     String data = convertStreamToString(is);
     browser=(WebView)findViewById(R.id.myWebView);
     browser.loadData(data,"text/html", "UTF-8");
} catch(Exception e){
    e.printStackTrace();
}

InputStream to String method:

private static String convertStreamToString(InputStream is) {

BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();

String line = null;
try {
    while ((line = reader.readLine()) != null) {
        sb.append((line + "\n"));
    }
} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        is.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
return sb.toString();

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文