更改Converter.Factory 加密
- Retrofit请求体加密,需要自己去写Factory
public class JsonConverterFactory extends Converter.Factory {
private static final String TAG = "JsonConverterFactory";
private final Gson gson;
public static JsonConverterFactory create() {
return create(new Gson());
}
public static JsonConverterFactory create(Gson gson) {
return new JsonConverterFactory(gson);
}
private JsonConverterFactory(Gson gson) {
if (gson == null) throw new NullPointerException("gson == null");
this.gson = gson;
}
@Nullable
@Override
public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
return new JsonRequestBodyConverter<>(gson, adapter);
}
@Nullable
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
return new JsonResponseBodyConverter<>(gson, adapter);
}
public static class JsonRequestBodyConverter<T> implements Converter<T, RequestBody> {
private static final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8");
private final Gson gson;
private final TypeAdapter<T> adapter;
public JsonRequestBodyConverter(Gson gson, TypeAdapter<T> adapter) {
this.gson = gson;
this.adapter = adapter;
}
@Override
public RequestBody convert(T value) throws IOException {
String byteDecrypt = DESUtils.encrypt(value.toString(), "");
return RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),byteDecrypt);
}
}
public class JsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {
private final Gson mGson;
private final TypeAdapter<T> adapter;
public JsonResponseBodyConverter(Gson gson, TypeAdapter<T> adapter) {
this.mGson = gson;
this.adapter = adapter;
}
@Override
public T convert(ResponseBody responseBody) throws IOException {
Reader reader = StringToReader(responseBody.string().toString().trim());
JsonReader jsonReader = gson.newJsonReader(reader);
try {
return adapter.read(jsonReader);
} finally {
reader.close();
jsonReader.close();
}
}
private Reader StringToReader(String json){
Reader reader = new StringReader(json);
return reader;
}
}
- 加密需要传入的参数是body
@POST(".....")
Call<ResponseBody> health_1(@Body String s);
- 用Retrofit 发起请求
Retrofit retrofit = new Retrofit.Builder().baseUrl("http:....")
.addConverterFactory(JsonConverterFactory.create())
.build();
Map<String, String> map = new HashMap<>();
map...
Gson gson = new Gson();
String s = gson.toJson(map);
Call<ResponseBody> call = retrofit.create(...).health_1(s);
call.enqueue(....);
另一种加密
- 正常写GsonConverterFactory.create()
Retrofit retrofit = new Retrofit.Builder().baseUrl("http:...")
.addConverterFactory(GsonConverterFactory.create())
.build();
- 正常写请求
@POST("...")
Call<ResponseBody>up_record(@Body RequestBody s);
- 发起请求
Retrofit retrofit = new Retrofit.Builder().baseUrl("http:...")
.addConverterFactory(GsonConverterFactory.create())
.build();
Map<String, String> map = new HashMap<>();
map....
Gson gson = new Gson();
String s = gson.toJson(map);
String s1 = DESUtils.encrypt(s,"");
RequestBody body = RequestBody.create(MediaType.parse("application/json"), s1);
Call<ResponseBody> call = retrofit.create(IHP.class).up_record(body);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
String decryptString = DESUtils.decrypt(response.body().string(), "");
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
}
});
另:传参是json类型 也用@Body的方式传参
@Body参数提交的类型是'application/json'