Retrofit(okhttp)的动态代理
2017-11-14 10:03
260 查看
自己手动写一个代理模式
代码实现:
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Created by czx on 2017/11/14.
*/
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Get {
String value() default "";
}
定义一个接口
public interface INews {
@Get("http://www.qq.com")
String getQQ();
@Get("http://www.sina.com.cn")
String getSina();
}
写一个类
import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
* Created by czx on 2017/11/14.
*/
public class Retrofit {
public <T> T create(Class<T> service){
return (T) Proxy.newProxyInstance(service.getClassLoader(), new Class<?>[]{service}, new InvocationHandler() {
@Override
public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
Get get = method.getAnnotation(Get.class);
if(get == null){
return null;
}
String url = get.value();
return http(url);
}
});
}
public String http(String url) throws IOException {
Request request = new Request.Builder().url(url).build();
OkHttpClient client = new OkHttpClient();
Call call = client.newCall(request);
Response response = call.execute();
return response.body().string();
}
}
main方法里:
Retrofit retrofit = new Retrofit();
INews news = retrofit.create(INews.class);
String html = news.getSina();
System.out.println(html);
OK。
代码实现:
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Created by czx on 2017/11/14.
*/
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Get {
String value() default "";
}
定义一个接口
public interface INews {
@Get("http://www.qq.com")
String getQQ();
@Get("http://www.sina.com.cn")
String getSina();
}
写一个类
import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
* Created by czx on 2017/11/14.
*/
public class Retrofit {
public <T> T create(Class<T> service){
return (T) Proxy.newProxyInstance(service.getClassLoader(), new Class<?>[]{service}, new InvocationHandler() {
@Override
public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
Get get = method.getAnnotation(Get.class);
if(get == null){
return null;
}
String url = get.value();
return http(url);
}
});
}
public String http(String url) throws IOException {
Request request = new Request.Builder().url(url).build();
OkHttpClient client = new OkHttpClient();
Call call = client.newCall(request);
Response response = call.execute();
return response.body().string();
}
}
main方法里:
Retrofit retrofit = new Retrofit();
INews news = retrofit.create(INews.class);
String html = news.getSina();
System.out.println(html);
OK。
相关文章推荐
- Retrofit,Okhttp对每个Request统一动态添加header和参数(五)
- Retrofit,Okhttp对每个Request统一动态添加header和参数(五)
- Retrofit,Okhttp对每个Request统一动态添加header和参数
- Retrofit,Okhttp对每个Request统一动态添加header和参数(五)
- Retrofit2 完全解析 探索与okhttp之间的关系(三)
- Retrofit2 完全解析 探索与okhttp之间的关系
- Retrofit2 完全解析 探索与okhttp之间的关系
- RxAndroid+Retrofit+MVVM(1)OKHttp
- Retrofit2 完全解析 探索与okhttp之间的关系(一)
- Retrofit(OKHttp)多BaseUrl情况下url实时自动替换完美解决方法
- Retrofit2 完全解析 探索与okhttp之间的关系
- Retrofit2 完全解析 探索与okhttp之间的关系
- Retrofit/Okhttp API接口加固技术实践(上)
- Rxjava_Retrofit_Okhttp
- https 学习笔记以及在 retrofit/okhttp 中的使用 (超多图, 爪机慎入)
- Android Retrofit2&OkHttp3添加统一的请求头Header
- MVP,购物车啊,图片加载用Fresco,OKhttp+Retrofit实现
- android,retrofit,okhttp,日志拦截器,使用拦截器Interceptor统一打印请求与响应的json
- Volley与Retrofit(OKHttp)使用与区别
- Retrofit/Okhttp Https兼容到Http降级处理实践(信任所有https)