Android Webview posturl 传参被encode问题
生活随笔
收集整理的這篇文章主要介紹了
Android Webview posturl 传参被encode问题
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Android Webview posturl 傳參encode問題
????? 1、起因,閑話少說,近期因項目需求,需要在webview必須通過post方式加載url并傳參,自然想到posturl(url,byte[])
?????? 2、問題,起初很簡單,JSON拼接參數(shù),然后轉(zhuǎn)String,最后String.getBytes(),自然得到了byte[],服務器也很順利接收到了,可是呢順利的話應該是一個正常的json串,直接解析就能得到數(shù)據(jù),服務端接收到的json串被encode了,例如{}、""等都被encode了,本來呢直接decode也可以解決,偏偏IOS端是正常的,想辦法。
?????? 3、解決問題,途徑1:通過httpPost將數(shù)據(jù)post上去,再接收httpresponse,最后拿到response的數(shù)據(jù)放到webview去加載,前期post跟接收response輕易解決,但是偏偏在解析response加載到webview的時候又出現(xiàn)問題,試著debug調(diào)試,未果,索性研究其他辦法。
???????????? 途徑2:還是webview.posturl,區(qū)別在于得到json串以后不直接轉(zhuǎn)String,而是通過StringEntity,詳情見以下代碼 ??? StringEntity se = null;try{se = new StringEntity(jsonObject.toString(),"UTF-8");se.setContentType("application/json");byte[] array = EntityUtils.toByteArray(se);webView.postUrl(url, array);}catch (Exception e){e.printStackTrace();}
???????????? 途徑3:重寫 webview 的 shouldInterceptRequest,針對特定請求修改請求頭,詳見以下代碼
@Override public WebResourceResponse shouldInterceptRequest(WebView view, String url) {try {if (url.equals("http://192.168.0.12:8080/tt.php")) {URL url1 = new URL(url);HttpURLConnection conn = (HttpURLConnection) url1.openConnection();conn.setRequestMethod("POST");conn.setRequestProperty("Content-type", "application/json");String data = "{\"name\":\"test\"}";conn.getOutputStream().write(data.getBytes());WebResourceResponse resp = new WebResourceResponse("text/html",conn.getHeaderField("encoding"),conn.getInputStream());return resp;}}catch(Exception e){}return super.shouldInterceptRequest(view, url); }
為了方便測試,在本機搭了個簡單的服務器,直接生成一個servlet,修改web.xml映射,保證手機電腦在一個網(wǎng)段,在手機端直接訪問電腦ip/projectname/servlet映射名稱,例如 192.168.0.123/ServerDemo/myServlet,在servlet的dopost方法中解析就可以看到在安卓端webview通過post傳遞的參數(shù)
???
????? 1、起因,閑話少說,近期因項目需求,需要在webview必須通過post方式加載url并傳參,自然想到posturl(url,byte[])
?????? 2、問題,起初很簡單,JSON拼接參數(shù),然后轉(zhuǎn)String,最后String.getBytes(),自然得到了byte[],服務器也很順利接收到了,可是呢順利的話應該是一個正常的json串,直接解析就能得到數(shù)據(jù),服務端接收到的json串被encode了,例如{}、""等都被encode了,本來呢直接decode也可以解決,偏偏IOS端是正常的,想辦法。
?????? 3、解決問題,途徑1:通過httpPost將數(shù)據(jù)post上去,再接收httpresponse,最后拿到response的數(shù)據(jù)放到webview去加載,前期post跟接收response輕易解決,但是偏偏在解析response加載到webview的時候又出現(xiàn)問題,試著debug調(diào)試,未果,索性研究其他辦法。
???????????? 途徑2:還是webview.posturl,區(qū)別在于得到json串以后不直接轉(zhuǎn)String,而是通過StringEntity,詳情見以下代碼 ??? StringEntity se = null;try{se = new StringEntity(jsonObject.toString(),"UTF-8");se.setContentType("application/json");byte[] array = EntityUtils.toByteArray(se);webView.postUrl(url, array);}catch (Exception e){e.printStackTrace();}
???????????? 途徑3:重寫 webview 的 shouldInterceptRequest,針對特定請求修改請求頭,詳見以下代碼
@Override public WebResourceResponse shouldInterceptRequest(WebView view, String url) {try {if (url.equals("http://192.168.0.12:8080/tt.php")) {URL url1 = new URL(url);HttpURLConnection conn = (HttpURLConnection) url1.openConnection();conn.setRequestMethod("POST");conn.setRequestProperty("Content-type", "application/json");String data = "{\"name\":\"test\"}";conn.getOutputStream().write(data.getBytes());WebResourceResponse resp = new WebResourceResponse("text/html",conn.getHeaderField("encoding"),conn.getInputStream());return resp;}}catch(Exception e){}return super.shouldInterceptRequest(view, url); }
為了方便測試,在本機搭了個簡單的服務器,直接生成一個servlet,修改web.xml映射,保證手機電腦在一個網(wǎng)段,在手機端直接訪問電腦ip/projectname/servlet映射名稱,例如 192.168.0.123/ServerDemo/myServlet,在servlet的dopost方法中解析就可以看到在安卓端webview通過post傳遞的參數(shù)
???
總結(jié)
以上是生活随笔為你收集整理的Android Webview posturl 传参被encode问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 美团和大众点评合并后,两家的技术大牛们该
- 下一篇: js逆向爬虫某openLaw网站