【问题标题】:Android WebView.postUrl() showing blank screen when posting to HTTPS URL发布到 HTTPS URL 时,Android WebView.postUrl() 显示空白屏幕
【发布时间】:2011-10-04 10:54:58
【问题描述】:

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

【问题讨论】:

标签: android https httpclient android-webview


【解决方案1】:

试试这个

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

}

【讨论】:

    猜你喜欢
    • 2013-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-03
    • 2013-10-30
    • 1970-01-01
    • 1970-01-01
    • 2017-01-13
    相关资源
    最近更新 更多