- 1.定义一个网络访问的接口,
public interface IHttpProcessor {
void post(String url, Map params,ICallback callback);
}
- 2 .定义Icallback 回传结果
public interface ICallback {
void onSuccess(String result);
void onFailure();
}
- 3 .实现Icallback 接口 将string结果转成 穿过来要解析的对象
package com.example.amn.translucentdemo;
import com.google.gson.Gson;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
/**
* Created by Amn on 2019/4/19.
*/
public abstract class HttpCallback implements ICallback {
@Override
public void onSuccess(String result) {
//将数据转成json对象
Gson gson=new Gson();
Class> clz= analysisClassInfo(this);
Result objectResult= (Result) gson.fromJson(result,clz);
}
//获取 Result 对象
private Class> analysisClassInfo(Object object) {
//可以获取包含原始类型,参数化类型,类型变量,基本类型
Type genType=object.getClass().getGenericSuperclass();
Type[] typeArgrument=((ParameterizedType)genType).getActualTypeArguments();
return (Class>) typeArgrument[0];
}
@Override
public void onFailure() {
}
public abstract void onSuccess(Result objResult);
}
- 4 创建代理类HpptHelper 通过持有第三方框架的引用去实现网络访问
package com.example.amn.translucentdemo;
import java.util.Map;
/**
* Created by Amn on 2019/4/19.
*/
public class HpptHelper implements IHttpProcessor{
private static HpptHelper instance;
private HpptHelper(){
}
public static HpptHelper getInstance(){
synchronized (HpptHelper.class){
if(instance == null){
instance=new HpptHelper();
}
}
return instance;
}
//持有第三方访问框架
private static IHttpProcessor mIhttpProcessor;
public static void init(IHttpProcessor httpProcessor){
mIhttpProcessor=httpProcessor;
}
@Override
public void post(String url, Map params, ICallback callback) {
mIhttpProcessor.post(url,params,callback);
}
}
-
- 第三方框架的具体实现类 实现 IHttpProcessor 如volley
package com.example.amn.translucentdemo;
import android.content.Context;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import java.util.Map;
/**
* Created by Amn on 2019/4/19.
*/
public class VolleyProcessor implements IHttpProcessor {
private static RequestQueue mQueue=null;
public VolleyProcessor(Context context){
mQueue= Volley.newRequestQueue(context);
}
@Override
public void post(String url, Map params, final ICallback callback) {
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener() {
@Override
public void onResponse(String response) {
callback.onSuccess(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
callback.onFailure();
}
});
mQueue.add(stringRequest);
}
}
okhttp 实现
package com.example.amn.translucentdemo;
import android.os.Handler;
import java.io.IOException;
import java.util.Map;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
/**
* Created by Amn on 2019/4/19.
*/
public class OkhhtpProcessor implements IHttpProcessor {
private OkHttpClient mOkhttpClient;
private Handler mHandler;
public OkhhtpProcessor(){
mOkhttpClient=new OkHttpClient();
mHandler=new Handler();
}
@Override
public void post(String url, Map params, final ICallback callback) {
RequestBody responseBody = appendBody(params);
final Request request=new Request.Builder().url(url).post(responseBody).build();
mOkhttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
if(response.isSuccessful()){
final String str=response.body().string();
mHandler.post(new Runnable() {
@Override
public void run() {
callback.onSuccess(str);
}
});
}
}
});
}
private RequestBody appendBody(Map params) {
FormBody.Builder body=new FormBody.Builder();
if(params ==null || params.isEmpty()){
return body.build();
}
for(Map.Entry entry : params.entrySet()){
body.add(entry.getKey(),entry.getValue().toString());
}
return body.build();
}
}
- 6 .使用 在application 初始化具体用哪个框架
package com.example.amn.translucentdemo;
import android.app.Application;
/**
* Created by Amn on 2019/4/19.
*/
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// HpptHelper.init(new VolleyProcessor(this));
HpptHelper.init(new OkhhtpProcessor());
}
}
- 7 . activity 调用
HpptHelper.getInstance().post(url, params, new HttpCallback() {
@Override
public void onSuccess(DataInfo objResult) {
}
});