gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:29:55 25 4
gpt4 key购买 nike

在我的 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.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的任何方法中吗?

最佳答案

试试这个

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

关于Android WebView.postUrl() 在发布到 HTTPS URL 时显示空白屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7646783/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com