Commit 32669913 by gao.chao

网络框架进行代理模式改造

parent f54efc6b
...@@ -22,8 +22,8 @@ import com.mayi.fastdevelop.base.BaseActivity; ...@@ -22,8 +22,8 @@ import com.mayi.fastdevelop.base.BaseActivity;
import com.mayi.fastdevelop.bean.UserInfo; import com.mayi.fastdevelop.bean.UserInfo;
import com.mayi.fastdevelop.comnon.Constant; import com.mayi.fastdevelop.comnon.Constant;
import com.mayi.fastdevelop.comnon.RequestCode; import com.mayi.fastdevelop.comnon.RequestCode;
import com.mayi.fastdevelop.okhttp.NetWorkBuilder; import com.mayi.fastdevelop.http.NetWorkBuilder;
import com.mayi.fastdevelop.okhttp.ResultCallback; import com.mayi.fastdevelop.http.ResultCallback;
import com.mayi.fastdevelop.util.LogUtils; import com.mayi.fastdevelop.util.LogUtils;
import com.mayi.fastdevelop.view.OnMultiClickListener; import com.mayi.fastdevelop.view.OnMultiClickListener;
......
...@@ -10,6 +10,8 @@ import android.os.HandlerThread; ...@@ -10,6 +10,8 @@ import android.os.HandlerThread;
import androidx.multidex.MultiDexApplication; import androidx.multidex.MultiDexApplication;
import com.mayi.fastdevelop.http.HttpProxy;
import com.mayi.fastdevelop.http.okhttp.OKHttpModel;
import com.mayi.fastdevelop.util.ActivitysManager; import com.mayi.fastdevelop.util.ActivitysManager;
import com.mayi.fastdevelop.util.DateUtil; import com.mayi.fastdevelop.util.DateUtil;
import com.mayi.fastdevelop.util.FileUtil; import com.mayi.fastdevelop.util.FileUtil;
...@@ -51,6 +53,7 @@ public abstract class BaseApplication extends MultiDexApplication { ...@@ -51,6 +53,7 @@ public abstract class BaseApplication extends MultiDexApplication {
initLog(); initLog();
initSmartRefreshLayout(); initSmartRefreshLayout();
MMKV.initialize(this); MMKV.initialize(this);
HttpProxy.getInstance().init(new OKHttpModel());
} }
private void initSmartRefreshLayout() { private void initSmartRefreshLayout() {
......
package com.mayi.fastdevelop.http;
import java.io.File;
public class HttpProxy {
private static HttpProxy instance;
private IHttp iHttp;
public static HttpProxy getInstance() {
if (instance == null) {
instance = new HttpProxy();
}
return instance;
}
public void init(IHttp iHttp) {
this.iHttp = iHttp;
}
//上传文件
public void uploadFile(String url, File file, ResultCallback callback) {
if (iHttp != null) {
iHttp.uploadFile(url, file, callback);
}
}
//发起get请求
public void performSynGetHttpRequest(String url, ResultCallback callback) {
if (iHttp != null) {
iHttp.performSynGetHttpRequest(url, callback);
}
}
// //发起post请求
public void performSynPostHttpRequest(String url, String json, ResultCallback callback) {
if (iHttp != null) {
iHttp.performSynPostHttpRequest(url, json, callback);
}
}
}
package com.mayi.fastdevelop.http;
import java.io.File;
public interface IHttp {
//上传文件
public void uploadFile(String url, File file, ResultCallback callback);
//发起get请求
public void performSynGetHttpRequest(String url, ResultCallback callback);
//发起post请求
public void performSynPostHttpRequest(String url, String json, ResultCallback callback);
}
package com.mayi.fastdevelop.okhttp; package com.mayi.fastdevelop.http;
import android.text.TextUtils; import android.text.TextUtils;
...@@ -71,18 +71,18 @@ public class NetWorkBuilder { ...@@ -71,18 +71,18 @@ public class NetWorkBuilder {
//发起post请求 //发起post请求
private void sendPost() { private void sendPost() {
if (!TextUtils.isEmpty(stringParam)) { if (!TextUtils.isEmpty(stringParam)) {
NetWorkUtil.getInstance().performSynPostHttpRequest(url, stringParam, callback); HttpProxy.getInstance().performSynPostHttpRequest(url, stringParam, callback);
} else if (params.toString().length() > 2) { } else if (params.toString().length() > 2) {
NetWorkUtil.getInstance().performSynPostHttpRequest(url, params.toString(), callback); HttpProxy.getInstance().performSynPostHttpRequest(url, params.toString(), callback);
} else { } else {
NetWorkUtil.getInstance().performSynPostHttpRequest(url, null, callback); HttpProxy.getInstance().performSynPostHttpRequest(url, null, callback);
} }
} }
//发起get请求 //发起get请求
private void sendGet() { private void sendGet() {
if (!TextUtils.isEmpty(stringParam)) { if (!TextUtils.isEmpty(stringParam)) {
NetWorkUtil.getInstance().performSynGetHttpRequest(url + "?" + stringParam, callback); HttpProxy.getInstance().performSynGetHttpRequest(url + "?" + stringParam, callback);
} else if (params.toString().length() > 2) { } else if (params.toString().length() > 2) {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
builder.append("?"); builder.append("?");
...@@ -97,9 +97,9 @@ public class NetWorkBuilder { ...@@ -97,9 +97,9 @@ public class NetWorkBuilder {
} }
String s = builder.toString(); String s = builder.toString();
s.substring(0, s.length() - 1); s.substring(0, s.length() - 1);
NetWorkUtil.getInstance().performSynGetHttpRequest(url + s, callback); HttpProxy.getInstance().performSynGetHttpRequest(url + s, callback);
} else { } else {
NetWorkUtil.getInstance().performSynGetHttpRequest(url, callback); HttpProxy.getInstance().performSynGetHttpRequest(url, callback);
} }
} }
......
package com.mayi.fastdevelop.okhttp; package com.mayi.fastdevelop.http;
import java.lang.reflect.ParameterizedType; import java.lang.reflect.ParameterizedType;
......
package com.mayi.fastdevelop.okhttp; package com.mayi.fastdevelop.http.okhttp;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
......
package com.mayi.fastdevelop.okhttp; package com.mayi.fastdevelop.http.okhttp;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.text.TextUtils; import android.text.TextUtils;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.mayi.fastdevelop.http.IHttp;
import com.mayi.fastdevelop.http.ResultCallback;
import com.mayi.fastdevelop.util.LogUtils; import com.mayi.fastdevelop.util.LogUtils;
import com.tencent.bugly.crashreport.CrashReport; import com.tencent.bugly.crashreport.CrashReport;
...@@ -12,6 +14,7 @@ import org.json.JSONException; ...@@ -12,6 +14,7 @@ import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
...@@ -28,29 +31,44 @@ import okhttp3.RequestBody; ...@@ -28,29 +31,44 @@ import okhttp3.RequestBody;
import okhttp3.Response; import okhttp3.Response;
import okio.Buffer; import okio.Buffer;
public class OKHttpModel implements IHttp {
@Override
public void uploadFile(String url, File file, ResultCallback callback) {
}
public class NetWorkUtil { @Override
public void performSynGetHttpRequest(String url, ResultCallback callback) {
Request request = new Request.Builder().get()
.url(url)
.build();
initiateRequest(callback, request);
}
@Override
public void performSynPostHttpRequest(String url, String json, ResultCallback callback) {
if (TextUtils.isEmpty(json)) {
json = "";
}
RequestBody body = RequestBody.create(FORM_CONTENT_TYPE, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
initiateRequest(callback, request);
}
private static NetWorkUtil instance;
private OkHttpClient client; private OkHttpClient client;
private final MediaType FORM_CONTENT_TYPE private final MediaType FORM_CONTENT_TYPE
= MediaType.parse("application/json;charset=utf-8"); = MediaType.parse("application/json;charset=utf-8");
private Handler handler; private Handler handler;
private NetWorkUtil() { public OKHttpModel() {
client = new OkHttpClient.Builder() client = new OkHttpClient.Builder()
.connectTimeout(60, TimeUnit.SECONDS) .connectTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS).dns(new HttpDns()).build(); .readTimeout(60, TimeUnit.SECONDS).dns(new HttpDns()).build();
handler = new Handler(Looper.getMainLooper()); handler = new Handler(Looper.getMainLooper());
} }
public static NetWorkUtil getInstance() {
if (instance == null) {
instance = new NetWorkUtil();
}
return instance;
}
/** /**
* 将InputStream转换为String * 将InputStream转换为String
* *
...@@ -110,20 +128,6 @@ public class NetWorkUtil { ...@@ -110,20 +128,6 @@ public class NetWorkUtil {
// } // }
// } // }
//发起get请求
public void performSynGetHttpRequest(String url, ResultCallback callback) {
Request request = new Request.Builder().get()
.url(url)
.addHeader("content-type", "application/json")
.addHeader("__REQUEST_TYPE", "HTTP_CLIENT")
.addHeader("AUTH_TYPE", "1")
.addHeader("AUTH_TOKEN", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ7XCJ1c2VyQ29kZVwiOlwiOTA0NjM5NlwifSIsImlhdCI6MTU2MTQ1MTQwOX0.xizAX3iHVFMXNElpdMILZIYD7K7x8gaF2dG9J1hHPhE94e5a8fbea974e10aeebbe55eee21845")
.addHeader("x-auth-token","5fea181d-ec7f-4083-8647-3afa22268627")
.addHeader("USER_CODE", "9046396")
.build();
initiateRequest(callback, request);
}
private void initiateRequest(final ResultCallback callback, final Request request) { private void initiateRequest(final ResultCallback callback, final Request request) {
client.newCall(request).enqueue(new Callback() { client.newCall(request).enqueue(new Callback() {
@Override @Override
...@@ -144,7 +148,7 @@ public class NetWorkUtil { ...@@ -144,7 +148,7 @@ public class NetWorkUtil {
} }
Throwable throwable=new Throwable("网络异常",e); Throwable throwable=new Throwable("网络异常",e);
CrashReport.postCatchedException(throwable); CrashReport.postCatchedException(throwable);
LogUtils.netWorkFail("NetWorkUtil", call.request().url().toString(), bodyToString(call.request()), e); LogUtils.netWorkFail("HttpProxy", call.request().url().toString(), bodyToString(call.request()), e);
} }
@Override @Override
...@@ -185,24 +189,11 @@ public class NetWorkUtil { ...@@ -185,24 +189,11 @@ public class NetWorkUtil {
} }
}); });
} }
LogUtils.netWorkSuccess("NetWorkUtil", call.request().url().toString(), bodyToString(call.request()), json); LogUtils.netWorkSuccess("HttpProxy", call.request().url().toString(), bodyToString(call.request()), json);
} }
}); });
} }
//发起post请求
public void performSynPostHttpRequest(String url, String json, ResultCallback callback) {
if (TextUtils.isEmpty(json)) {
json = "";
}
RequestBody body = RequestBody.create(FORM_CONTENT_TYPE, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
initiateRequest(callback, request);
}
private boolean isText(MediaType mediaType) { private boolean isText(MediaType mediaType) {
if (mediaType.type() != null && mediaType.type().equals("text")) { if (mediaType.type() != null && mediaType.type().equals("text")) {
return true; return true;
...@@ -212,7 +203,7 @@ public class NetWorkUtil { ...@@ -212,7 +203,7 @@ public class NetWorkUtil {
mediaType.subtype().equals("xml") || mediaType.subtype().equals("xml") ||
mediaType.subtype().equals("html") || mediaType.subtype().equals("html") ||
mediaType.subtype().equals("webviewhtml") mediaType.subtype().equals("webviewhtml")
) )
return true; return true;
} }
return false; return false;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment