问题
I'm making an webview app for Android and iPhone.
In Android I'm facing an Issue
I have a webview with custom WebviewClient like this
webView.setWebViewClient(new MyWebViewClient());
here is my Custom WebviewClient class
private class MyWebViewClient extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
if (url.equalsIgnoreCase(MyConstants.fb_share)) {
view.stopLoading();
//Want to get POST parameters embedded with the url.
Intent i = new Intent(context, FaceBookActivity.class);
i.putExtra("FB_SHARE_URL", url);
startActivity(i);
}
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
loadedUrl = url;
return super.shouldOverrideUrlLoading(view, url);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
}
note: in onPageStarted() method I'm starting an other activity. But here I want to get the POST parameters with the URL. Is there any method to get the post parameters embedded with the URL?
In iPhone this is possible to get POST url parameters.
回答1:
It's not possible to do this (look at POST data) using the Android WebView since it doesn't allow you to look at the data being sent to the server. You could re-write the page with JavaScript fish out the values of the input fields (or change the method to GET) but handling images with either method will probably not be trivial.
Since you write 'POST parameters embedded with the url' I wonder if the form is using method='get', in which case take a look at this answer: Parsing query strings on Android
Also, don't call stopLoading
from onPageStarted
, that's going to end in a race. Use shouldOverrideUrlLoading
for that (just return true from there instead of calling stopLoading
).
来源:https://stackoverflow.com/questions/21089949/how-to-get-post-parameters-from-android-webviews-url-in-onpagestart-method