我还在拥抱Eclipse呐!!!
我使用的版本
retrofit-2.3.0.jar
okio-1.13.0.jar
okhttp-3.8.1.jar
目前项目现阶段使用的网络库是OKhttp3
现在将Retrofit2 加入到项目中,Retrofit2 OKhttp3 无缝链接么?确实是,代码还是要改动的,权当记录一下改动日志吧!!!
我们的接口不带多级目录,添加参数直接怼的形式
也许你们的接口是这样
https://api.github.com/repos/square/okhttp/issues
也许是这样
https://api.github.com/issues
我们的接口是这样,不带层级,不带层级,不带层级
https://api.github.com
一、请求路径构造
http://pan.baidu.com?data=加密json
public interface IBannerService {
@FormUrlEncoded
@POST("/")
public Call getBanner(@Field("data") String json);
}
public class BannerReq implements Serializable {
private static final long serialVersionUID = 1L;
public int num;
public String version="v1.0.0";
public String system = "360";
public BannerReq(int num) {
super();
this.num = num;
}
@Override
public String toString() {
return "BannerReq [num=" + num + ", version=" + version + ", system="
+ system + "]";
}
}
public class BannerResp implements Serializable {
private static final long serialVersionUID = 1L;
public int code;
public int num;
public String msg;
public List data;
public class Banner implements Serializable {
private static final long serialVersionUID = 1L;
public String id;
public String name;
@Override
public String toString() {
return "Banner [id=" + id + ", name=" + name + "]";
}
}
@Override
public String toString() {
return "BannerResp [code=" + code + ", num=" + num + ", msg=" + msg
+ ", data=" + data + "]";
}
}
二
、参数加密解密
public class IGsonFactory extends Converter.Factory {
public static IGsonFactory create() {
return create(new GsonBuilder().setLenient().create());
}
public static IGsonFactory create(Gson gson) {
if (gson == null)
throw new NullPointerException("gson == null");
return new IGsonFactory(gson);
}
private final Gson gson;
private IGsonFactory(Gson gson) {
this.gson = gson;
}
@Override
public Converter responseBodyConverter(Type type,
Annotation[] annotations, Retrofit retrofit) {
TypeAdapter> adapter = gson.getAdapter(TypeToken.get(type));
return new IResponseBodyConverter<>(gson, adapter); // 响应
}
@Override
public Converter, RequestBody> requestBodyConverter(Type type,
Annotation[] parameterAnnotations, Annotation[] methodAnnotations,
Retrofit retrofit) {
System.out.println("#发起请求#");
TypeAdapter> adapter = gson.getAdapter(TypeToken.get(type));
return new IRequestBodyConverter<>(gson, adapter); // 请求
}
}
public class IRequestBodyConverter implements Converter {
private static final MediaType MEDIA_TYPE = MediaType
.parse("application/json; charset=UTF-8");
static final Charset UTF_8 = Charset.forName("UTF-8");
final Gson gson;
final TypeAdapter adapter;
IRequestBodyConverter(Gson gson, TypeAdapter adapter) {
this.gson = gson;
this.adapter = adapter;
System.out.println("#IRequestBodyConverter初始化#");
}
@Override
public RequestBody convert(T value) throws IOException {
String json = value.toString();
System.out.println("#加密前#" + json);
json = AesEncryptionUtil.encrypt(json);
System.out.println("#加密后#" + json);
return RequestBody.create(MEDIA_TYPE, json);
}
}
package factory;
import java.io.IOException;
import okhttp3.ResponseBody;
import retrofit2.Converter;
import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.wyhd.encry.decry.security.util.AesEncryptionUtil;
public class IResponseBodyConverter implements Converter {
private final Gson gson;
private final TypeAdapter adapter;
IResponseBodyConverter(Gson gson, TypeAdapter adapter) {
this.gson = gson;
this.adapter = adapter;
}
@Override
public T convert(ResponseBody value) throws IOException {
String string = value.string();
System.out.println("#解密前#" + string);
string = AesEncryptionUtil.decrypt(string);
System.out.println("#解密后#" + string);
return adapter.fromJson(string);
}
}
三、调用方法
public class RetrofitHelper {
/** 基本路径 */
public static final String BASE_URL = "https://api.imeizan.cn";
public static void tst(String json) {
json = AesEncryptionUtil.encrypt(json);
Gson gson = new GsonBuilder().setLenient().create();
Retrofit retrofit = new Retrofit.Builder().baseUrl(BASE_URL)
.client(getOkHttpClient())
.addConverterFactory(IGsonFactory.create(gson)).build();
IBannerService service = retrofit.create(IBannerService.class);
Call call = service.getBanner(json);
call.enqueue(new Callback() {
@Override
public void onResponse(Call call,
Response resp) {
BannerResp body = resp.body();
System.out.println("异步返回:" + body.toString());
}
@Override
public void onFailure(Call msg, Throwable error) {
System.out.println(msg.toString() + "|" + error.getMessage());
}
});
final Call clone = call.clone();
new Thread() {
public void run() {
try {
Response execute = clone.execute();
System.out.println("同步返回:" + execute.body().toString());
} catch (Exception e) {
System.out.println("同步返回:" + e.getMessage());
}
};
}.start();
}
public static OkHttpClient getOkHttpClient() {
// 日志显示级别
HttpLoggingInterceptor.Level level = HttpLoggingInterceptor.Level.BODY;
// 新建log拦截器
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(
new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
System.out.println(message);
}
});
loggingInterceptor.setLevel(level);
// 定制OkHttp
OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
// OkHttp进行添加拦截器loggingInterceptor
httpClientBuilder.addInterceptor(loggingInterceptor);
return httpClientBuilder.build();
}
}
四:测试代码
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BannerReq req = new BannerReq(1);
Gson gson = new Gson();
String json = gson.toJson(req);
System.out.println(json);
RetrofitHelper.tst(json);
RetrofitHelper002.tst(json);
}
}