在我的Android应用中,我将数据发布到https
的{{1}} servlet网址,如下所示
WebView
上面代码中的URL是一个servlet URL,我必须发布一些数据,然后从那里重定向到其他一些URL。
当servlet URL只是String postData = "fileContents=" + fileCon;
WebView.postUrl(url, EncodingUtils.getBytes(postData, "BASE64"));
时,上面的代码工作正常。但是当更改为HTTP
时,它显示为空白屏幕。
我为android HTTPS
问题尝试了以下解决方案:
http://blog.antoine.li/index.php/2010/10/android-trusting-ssl-certificates/
我从HTTPS
方法中删除了上述代码,并尝试了以下代码
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();
}
或loadUrl
我看到空白屏幕了吗?
或者我应该将上述代码放在postUrl
的任何方法中?
答案 0 :(得分:0)
试试这个
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方法:
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();
}