HTTP POST在WebView中的响应android
问题描述:
我试图使用HTTP Post连接到页面。我做了一个http post创建 的web视图。我需要重定向到webview中的另一个页面。但是当继续按钮被点击时,抛出异常。HTTP POST在WebView中的响应android
我的代码是
public class ZHttpPostProjActivity extends Activity {
/** Called when the activity is first created. */
private WebView mWebView;
private ProgressDialog progressBar;
private static final String TAG = "ZHttpPostProjActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web_view);
mWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
progressBar = ProgressDialog.show(ZHttpPostProjActivity.this, "",
"Loading...");
postData();
}
private final String URL_REGISTER = "https://www.paypal.com/checkout";
public void postData() {
BufferedReader bufferedReader = null;
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("username", "username"));
nameValuePairs.add(new BasicNameValuePair("password", "password"));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URL_REGISTER);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,
HTTP.UTF_8));
HttpResponse response = httpclient.execute(httppost);
bufferedReader = new BufferedReader(new InputStreamReader(response
.getEntity().getContent()));
StringBuffer stringBuffer = new StringBuffer("");
String line = "";
String LineSeparator = System.getProperty("line.separator");
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line + LineSeparator);
}
bufferedReader.close();
String webData = stringBuffer.toString();
Log.i(TAG + "web data : ", webData);
// String webData = new
// BasicResponseHandler().handleResponse(response);
Log.i(TAG, "Httppost.getURI().toString(): "
+ httppost.getURI().toString());
mWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i(TAG, "Processing webview url click...");
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
Log.i(TAG, "Finished loading URL: " + url);
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
Log.e(TAG, "Error: " + description + " \n errorCode: "
+ errorCode + "\n failingUrl: " + failingUrl);
}
});
// mWebView.loadUrl(httppost.getURI().toString());
mWebView.loadData(webData, "text/html", "UTF-8");
mWebView.loadDataWithBaseURL(httppost.getURI().toString(), webData,
"text/html", HTTP.UTF_8, null);
} catch (Exception ex) {
ex.printStackTrace();
}
}
同时单击继续按钮,在web视图抛出一个异常
02-21 11:42:38.539: E/webviewdatabase(2848): Failed in setFormData
02-21 11:42:38.539: E/webviewdatabase(2848): java.net.MalformedURLException: Unknown protocol: about
02-21 11:42:38.539: E/webviewdatabase(2848): at java.net.URL.<init>(URL.java:288)
02-21 11:42:38.539: E/webviewdatabase(2848): at java.net.URL.<init>(URL.java:157)
02-21 11:42:38.539: E/webviewdatabase(2848): at android.webkit.WebViewDatabase.setFormData(WebViewDatabase.java:1032)
02-21 11:42:38.539: E/webviewdatabase(2848): at android.webkit.BrowserFrame.loadStarted(BrowserFrame.java:384)
02-21 11:42:38.539: E/webviewdatabase(2848): at android.webkit.JWebCoreJavaBridge.sharedTimerFired(Native Method)
02-21 11:42:38.539: E/webviewdatabase(2848): at android.webkit.JWebCoreJavaBridge.fireSharedTimer(JWebCoreJavaBridge.java:91)
02-21 11:42:38.539: E/webviewdatabase(2848): at android.webkit.JWebCoreJavaBridge.handleMessage(JWebCoreJavaBridge.java:108)
02-21 11:42:38.539: E/webviewdatabase(2848): at android.os.Handler.dispatchMessage(Handler.java:99)
02-21 11:42:38.539: E/webviewdatabase(2848): at android.os.Looper.loop(Looper.java:123)
02-21 11:42:38.539: E/webviewdatabase(2848): at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:673)
02-21 11:42:38.539: E/webviewdatabase(2848): at java.lang.Thread.run(Thread.java:1019)
02-21 11:42:41.324: E/cache(2848): illegal expires: Sat, Jan 01 2000 01:01:01 GMT
答
使用这样
WebView webview = new WebView(this);
setContentView(webview);
byte[] post = EncodingUtils.getBytes("postvariable=value&nextvar=value2", "BASE64");
webview.postUrl("http://www.geenie.nl/AnHeli/mobile/ranking/demo/index.php", post);
答
我不知道,如果这是你的失败,或者这是否会导致一个错误,但是因为你正在做一个POST,删除“?”在URL_REGISTER变量的末尾。
private final String URL_REGISTER = "https://www.paypal.com/checkout?";
“?”只有在执行GET请求时才需要。
大卫
+1
我删除了 “?” 。在调用Http帖子后,第一个成功加载的URL,将从“https://www.paypal.com/checkout”页面重定向到https://www.paypal.com/conformation的setFormData异常抛出失败 – jennifer 2012-02-21 06:51:26
在生成后数据字符串时使用'URLEncoder.encode(密码,“UTF-8”)'。 – 2017-11-11 14:56:45