Browse Source

添加了一个webReqeust网络请求函数

lyn 5 years ago
parent
commit
f791d80ab9

+ 100 - 2
app/src/main/java/com/haochuan/hciptvbasic/Util/ToolsUtil.java

@@ -7,15 +7,21 @@ import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;
 import android.net.Uri;
 import android.os.Bundle;
+import android.text.TextUtils;
 import android.util.Base64;
+import android.util.Log;
+import android.webkit.JavascriptInterface;
 
+import com.haochuan.hciptvbasic.web.MyRequest;
 import com.yanzhenjie.nohttp.NoHttp;
 import com.yanzhenjie.nohttp.RequestMethod;
 import com.yanzhenjie.nohttp.rest.OnResponseListener;
+import com.yanzhenjie.nohttp.rest.Request;
 import com.yanzhenjie.nohttp.rest.StringRequest;
 
 import org.json.JSONObject;
 
+import java.net.URLEncoder;
 import java.util.Iterator;
 import java.util.Set;
 
@@ -93,8 +99,8 @@ public class ToolsUtil {
      *@param ignoreResult 是否忽略结果,true,忽略;false,不忽略.
      *@param tag 透传参数,将在结果回调中一并返回,主要区别多个并发请求
      * */
-    public void clientWebRequest(String url,String paramJson,
-                                 String headJson,int method, boolean ignoreResult,
+    public void clientWebRequest(String url,int method,String paramJson,
+                                 String headJson, boolean ignoreResult,
                                  String tag,IResponseListener listener){
         if(url == null || paramJson == null || headJson == null || tag == null){
             Logger.w(String.format("参数不能为null,url:%s;paramJson:%s;headJson:%s;" +
@@ -174,6 +180,98 @@ public class ToolsUtil {
     }
 
 
+    public void clientWebRequest(String url, int method,
+                                 String contentType,String headJson,
+                                 String paramsBody, boolean ignoreResult,
+                                 String tag,IResponseListener listener){
+        try{
+            Logger.d(String.format("webRequest,url:%s,method:%s,contentType:%s," +
+                            "headJson:%s,paramsBody:%s,tag:%s,ignoreResult%s,",
+                    url,method,contentType,headJson,paramsBody,tag,ignoreResult?"忽略":"不忽略"));
+            RequestMethod requestMethod ;
+            switch (method){
+                case 1:
+                    requestMethod = RequestMethod.GET;
+                    break;
+                case 2:
+                    requestMethod = RequestMethod.POST;
+                    break;
+                default:
+                    requestMethod = RequestMethod.GET;
+                    break;
+            }
+            Request<String> request = new MyRequest(url, requestMethod);
+
+            //添加头部
+            if(!TextUtils.isEmpty(headJson)){
+                JSONObject headParams = new JSONObject(headJson);
+                Iterator<String> headIterators = headParams.keys();
+                while (headIterators.hasNext()){
+                    String paramKey = headIterators.next();
+                    String paramValue = headParams.getString(paramKey);
+                    request.addHeader(paramKey,paramValue);
+                }
+            }
+
+            if(TextUtils.equals(contentType,"application/x-www-form-urlencoded")){
+                JSONObject jsonObject = new JSONObject(paramsBody);
+                Iterator iterator =  jsonObject.keys();
+                StringBuilder sb = new StringBuilder();
+                int i = 0;
+                while (iterator.hasNext()){
+                    if(i>0){
+                        sb.append("&");
+                    }
+                    String key = (String)iterator.next();
+                    String value = jsonObject.getString(key);
+                    value = URLEncoder.encode(value,"UTF-8");
+                    sb.append(String.format("%s=%s",key,value));
+                    i++;
+                }
+                paramsBody = sb.toString();
+            }
+
+            if(method == 2 && !TextUtils.isEmpty(paramsBody)){
+                request.setDefineRequestBody(paramsBody,contentType);
+            }
+            NoHttp.newRequestQueue().add(100, request, new OnResponseListener<String>(){
+                @Override
+                public void onStart(int what){
+                    Logger.d("clientWebRequest,开始请求");
+                }
+
+                @Override
+                public void onSucceed(int what, com.yanzhenjie.nohttp.rest.Response<String> response){
+                    String data = response.get();
+                    String result = ignoreResult ? "{}" : data.replace("\"", "\\\"");
+                    result = result.replace("\n","");
+                    String base64Response = Base64.encodeToString(data.getBytes(),Base64.NO_WRAP);
+                    base64Response = base64Response.replace("\n","");
+                    if(ignoreResult){
+                        listener.OnResponse(0,"{}",tag);
+                    }else{
+                        listener.OnResponse(0,base64Response,tag);
+                    }
+                }
+
+                @Override
+                public void onFailed(int what, com.yanzhenjie.nohttp.rest.Response<String> response) {
+                    Logger.w("clientWebRequest,请求认证失败:" + what);
+                    listener.OnResponse(-1,"{}",tag);
+                }
+
+                @Override
+                public void onFinish(int what){
+                    Logger.d("clientWebRequest,请求认证结束");
+                }
+            });
+        }catch(Exception e){
+            Logger.e("JS传递的请求发生未知异常");
+            e.printStackTrace();
+        }
+    }
+
+
 
     /*
     * clientWebRequest 结果response接口

+ 26 - 0
app/src/main/java/com/haochuan/hciptvbasic/web/MyRequest.java

@@ -0,0 +1,26 @@
+package com.haochuan.hciptvbasic.web;
+
+import com.yanzhenjie.nohttp.Headers;
+import com.yanzhenjie.nohttp.RequestMethod;
+import com.yanzhenjie.nohttp.rest.Request;
+import com.yanzhenjie.nohttp.tools.HeaderUtils;
+import com.yanzhenjie.nohttp.tools.IOUtils;
+
+public class MyRequest extends Request<String> {
+    public MyRequest(String url) {
+        super(url, RequestMethod.GET);
+    }
+
+    public MyRequest(String url, RequestMethod requestMethod) {
+        super(url, requestMethod);
+    }
+
+
+    @Override
+    public String parseResponse(Headers responseHeaders, byte[] responseBody) throws Exception {
+        if (responseBody == null || responseBody.length == 0)
+            return "";
+        String charset = HeaderUtils.parseHeadValue(responseHeaders.getContentType(), "charset", "");
+        return IOUtils.toString(responseBody, charset);
+    }
+}

+ 22 - 2
app/src/main/java/com/haochuan/hciptvbasic/webview/ToolToJS.java

@@ -200,8 +200,28 @@ public class ToolToJS {
      *@param tag 透传参数,将在结果回调中一并返回,主要区别多个并发请求
      * */
     @JavascriptInterface
-    public void clientWebRequest(String url,String paramJson,String headJson,int method,boolean ignoreResult,String tag){
-        toolsUtil.clientWebRequest(url, paramJson, headJson, method, ignoreResult, tag,
+    public void clientWebRequest(String url,int method,String paramJson,String headJson,boolean ignoreResult,String tag){
+        toolsUtil.clientWebRequest(url,method, paramJson, headJson,  ignoreResult, tag,
+                (int what,String response,String tag1)->{
+                    Logger.d(String.format("what:%s,response:%s;tag:%s",what,response,tag1));
+                    JsUtil.evaluateJavascript(context,webView,
+                            String.format(JS_EVENT_RESPONSE,what,response,tag1));
+                });
+    }
+
+
+    /*
+     *js 通过apk客户端访问网络接口
+     *@param contentType 网络类型,详情参考网络请求的content-type
+     *@param paramJson 请求参数集,格式为json字符串
+     *@param headJson 请求头部集,格式为json字符串
+     *@param method 请求方法,1,get;2,post
+     *@param ignoreResult 是否忽略结果,true,忽略;false,不忽略.
+     *@param tag 透传参数,将在结果回调中一并返回,主要区别多个并发请求
+     * */
+    @JavascriptInterface
+    public void clientWebRequest(String url,int method,String contentType,String paramJson,String headJson,boolean ignoreResult,String tag){
+        toolsUtil.clientWebRequest(url, method, contentType, headJson,paramJson, ignoreResult, tag,
                 (int what,String response,String tag1)->{
                     Logger.d(String.format("what:%s,response:%s;tag:%s",what,response,tag1));
                     JsUtil.evaluateJavascript(context,webView,