Sre*_*ram 5 https android httpclient android-webview
在我的Android应用程序中,我将数据发送到httpsservlet URL WebView,如下所示
String postData = "fileContents=" + fileCon;
WebView.postUrl(url, EncodingUtils.getBytes(postData, "BASE64"));
Run Code Online (Sandbox Code Playgroud)
上面代码中的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();
}
Run Code Online (Sandbox Code Playgroud)
现在我可以发布数据,并从那里也重定向.但我仍然看到一个空白的屏幕.
是因为我没有loadUrl或postUrl我看到空白屏幕?
或者我应该将上述代码放在任何方法中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();
}
Run Code Online (Sandbox Code Playgroud)
输入流转字符串方法:
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();
Run Code Online (Sandbox Code Playgroud)
}
| 归档时间: |
|
| 查看次数: |
2320 次 |
| 最近记录: |