Android – 检查请求是GET还是POST

前端之家收集整理的这篇文章主要介绍了Android – 检查请求是GET还是POST前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我需要在我的Android应用程序中检查请求是否是POST或者在shouldInterceptRequest中的GET.
见下面的代码

  1. public class CustomWebViewClient extends WebViewClient {
  2. ...
  3. @Override
  4. public WebResourceResponse shouldInterceptRequest(WebView view,String url) {
  5. if ("request is POST")
  6. Log.d("CustomWebViewClient","request is a POST");
  7. else if ("request is GET")
  8. Log.d("CustomWebViewClient","request is a GET");
  9. ...
  10. }
  11. }

是否有可能在WebViewClient的范围内确定这一点?

最佳答案
可以通过扩展WebViewClient来实现,但它可能涉及比预期更多的工作. WebViewClient中的回调方法由JNI调用,您无法调用获取标头和方法,因此最好的办法是使用JavaScript.

解决方案基于kristof对http://code.google.com/p/android/issues/detail?id=9122#c21评论

1.创建一个名为post_interceptor.js的文件,并将其放在res / raw中

post_interceptor.js

  1. HTMLFormElement.prototype._submit = HTMLFormElement.prototype.submit;
  2. HTMLFormElement.prototype.submit = interceptor;
  3. window.addEventListener('submit',function(e) {
  4. interceptor(e);
  5. },true);
  6. function interceptor(e) {
  7. var frm = e ? e.target : this;
  8. interceptor_onsubmit(frm);
  9. frm._submit();
  10. }
  11. function interceptor_onsubmit(f) {
  12. var jsonArr = [];
  13. for (i = 0; i < f.elements.length; i++) {
  14. var parName = f.elements[i].name;
  15. var parValue = f.elements[i].value;
  16. var parType = f.elements[i].type;
  17. jsonArr.push({
  18. name : parName,value : parValue,type : parType
  19. });
  20. }
  21. window.interception.customSubmit(JSON.stringify(jsonArr),f.attributes['method'] === undefined ? null : f.attributes['method'].nodeValue,f.attributes['enctype'] === undefined ? null : f.attributes['enctype'].nodeValue);
  22. }
  23. lastXmlhttpRequestPrototypeMethod = null;
  24. XMLHttpRequest.prototype.reallyOpen = XMLHttpRequest.prototype.open;
  25. XMLHttpRequest.prototype.open = function(method,url,async,user,password) {
  26. lastXmlhttpRequestPrototypeMethod = method;
  27. this.reallyOpen(method,password);
  28. };
  29. XMLHttpRequest.prototype.reallySend = XMLHttpRequest.prototype.send;
  30. XMLHttpRequest.prototype.send = function(body) {
  31. window.interception.customAjax(lastXmlhttpRequestPrototypeMethod,body);
  32. lastXmlhttpRequestPrototypeMethod = null;
  33. this.reallySend(body);
  34. };

2.创建一个名为JavascriptPostIntercept的Java类

根据需要更改包/类名.

JavascriptPostIntercept.java

  1. public class JavascriptPostIntercept {
  2. public interface JavascriptPostInterceptInterface {
  3. public void nextMessageIsAjaxRequest(AjaxRequestContents contents);
  4. public void nextMessageIsFormRequest(FormRequestContents contents);
  5. }
  6. private static String sInterceptHeader;
  7. private JavascriptPostInterceptInterface mClient;
  8. public static String getInterceptHeader() {
  9. if (sInterceptHeader == null) {
  10. // Assuming you have your own stream to string implementation
  11. sInterceptHeader = StringUtils.readInputStream(
  12. Resources.getSystem().openRawResource(R.raw.post_interceptor));
  13. }
  14. return sInterceptHeader;
  15. }
  16. public static class AjaxRequestContents {
  17. private String mMethod;
  18. private String mBody;
  19. public AjaxRequestContents(String method,String body) {
  20. mMethod = method;
  21. mBody = body;
  22. }
  23. public String getMethod() {
  24. return mMethod;
  25. }
  26. public String getBody() {
  27. return mBody;
  28. }
  29. }
  30. public static class FormRequestContents {
  31. private String mJson;
  32. private String mMethod;
  33. private String mEnctype;
  34. public FormRequestContents(String json,String method,String enctype) {
  35. mJson = json;
  36. mMethod = method;
  37. mEnctype = enctype;
  38. }
  39. public String getJson() {
  40. return mJson;
  41. }
  42. public String getMethod() {
  43. return mMethod;
  44. }
  45. public String getEnctype() {
  46. return mEnctype;
  47. }
  48. }
  49. public JavascriptPostIntercept(JavascriptPostInterceptInterface client) {
  50. mClient = client;
  51. }
  52. @JavascriptInterface
  53. public void customAjax(final String method,final String body) {
  54. mClient.nextMessageIsAjaxRequest(new AjaxRequestContents(method,body));
  55. }
  56. @JavascriptInterface
  57. public void customSubmit(String json,String enctype) {
  58. mClient.nextMessageIsFormRequest(new FormRequestContents(json,method,enctype));
  59. }
  60. }

3.创建WebViewClient子类

下面的代码获取最新请求的HTTP方法,这看起来足以满足您的要求,但很明显,AjaxRequestContents和FormSubmitContents上的其他方法可以让您在需要时访问帖子正文和其他内容.

  1. class MyWebViewClient extends WebViewClient implements JavascriptPostIntercept.JavascriptPostInterceptInterface {
  2. private String mLastRequestMethod = "GET";
  3. /// evaluate post_interceptor.js after the page is loaded
  4. @Override
  5. public void onPageFinished(WebView view,String url) {
  6. view.loadUrl("javascript: " + JavascriptPostIntercept.getInterceptHeader());
  7. }
  8. @TargetApi(11)
  9. @Override
  10. public WebResourceResponse shouldInterceptRequest(WebView view,String url) {
  11. if (mLastRequestMethod.equals("POST")) {
  12. // do stuff here...
  13. } else if (mLastRequestMethod.equals("GET")) {
  14. // do other stuff here...
  15. }
  16. // return something here...
  17. }
  18. @Override
  19. public void nextMessageIsAjaxRequest(JavascriptPostIntercept.AjaxRequestContents contents) {
  20. mLastRequestMethod = contents.getMethod();
  21. }
  22. @Override
  23. public void nextMessageIsFormRequest(JavascriptPostIntercept.FormRequestContents contents) {
  24. mLastRequestMethod = contents.getMethod();
  25. }
  26. }

4.创建适当的JS-Java链接

  1. MyWebViewClient webViewClient = new MyWebViewClient();
  2. mWebView.setWebViewClient(webViewClient);
  3. mWebView.addJavascriptInterface(new JavascriptPostIntercept(webViewClient),"interception");

猜你在找的Android相关文章