最近網絡網絡框架庫升級了一下。以前是1版本的組合。現在用這個rxjava2+okhttp3+retrofit2,有個習慣,把組合需要的庫都下載成離線。所以這這個組合如下:
api files('libs/rxjava-2.2.8.jar')
api files('libs/retrofit-2.3.0.jar')
api files('libs/okhttp-3.9.1.jar')
api files('libs/gson-2.6.2.jar')
api files('libs/converter-gson-2.3.0.jar')
api files('libs/adapter-rxjava2-2.3.0.jar')
api(name: 'rxandroid-2.0.2', ext: 'aar')
api files('libs/okio-1.15.0.jar')
上面庫整體打包的下載地址:https://download.csdn.net/download/qq_16064871/11409551
隨手附上一個使用示例:多參數請求和文件下載接口
public interface ManagerAPI {
@Multipart
@POST("/tap/exp/attendance")
Call<Attendance> attendanceSave(@PartMap Map<String, RequestBody> map, @Part MultipartBody.Part file);
}
返回的是直接json使用gson轉成的結構體。不用擔心解析,你可以在線json解析生成Java實體類。也可以用android studio的插件:
初始化:
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.connectTimeout(TIMEOUT, TimeUnit.SECONDS);
builder.readTimeout(TIMEOUT, TimeUnit.SECONDS);
builder.writeTimeout(TIMEOUT, TimeUnit.SECONDS);
Retrofit.Builder rBuilder2 = new Retrofit.Builder().client(builder.build()).addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJava2CallAdapterFactory.create()).baseUrl(mHostCould);
manageAPI = rBuilder2.build().create(ManagerAPI.class);
下面就是傳參,請求返回數據:
//構建要上傳的文件
Map<String, RequestBody> params = new HashMap<>();
params.put("code", RequestBody.create(MediaType.parse("text/plain"), device_code));
params.put("id", RequestBody.create(MediaType.parse("text/plain"), rid));
params.put("sign", RequestBody.create(MediaType.parse("text/plain"), sign));
params.put("times", RequestBody.create(MediaType.parse("text/plain"), timestamp));
MultipartBody.Part Part = MultipartBody.Part.createFormData("img", file.getName(), RequestBody.create(MediaType.parse("image/*"), file));
Call<Attendance> call = newUpdateAPI.attendanceSave(params, Part);
call.enqueue(new Callback<Attendance>() {
@Override
public void onResponse(Call<Attendance> call, Response<Attendance> response) {
if (response.code() == 200) {
attendance = response.body();
if (attendance != null) {
EventBus.getDefault().post(attendance);
}
}
}
@Override
public void onFailure(Call<Attendance> call, Throwable t) {
if(attendance != null) {
EventBus.getDefault().post(attendance);
}
}
});
這樣就完成了。
現在說說鍵值從小到大先後順序排序:
1、參數排序拼裝 根據參數名稱(除sign本身以外)將所有請求參數按照參數鍵值從小到大先後順序排序: key1=value1&key2=value2&key3=value3 ....(依次類推),最後拼接上&token=tokenValue;最後MD5加密得到簽名; 例如:需要請求接口傳參數 a=>"參數1",c=>“參數2”,b=>"參數3" 按參數鍵值排序後得到:a=參數1&b=參數2&c=參數3
2、在後面拼接token(假如token爲ddssf22sfsfs44fgjh55mh)得到:a=參數1&b=參數3&c=參數2×tamp=時間戳&token=ddssf22sfsfs44f5mh
3、MD5加密得到字符串簽名
代碼實現:
public static String createSign2(SortedMap<Object, Object> parameters, String key) {
StringBuffer sb = new StringBuffer();
StringBuffer sbkey = new StringBuffer();
Set es = parameters.entrySet(); //所有參與傳參的參數按照accsii排序(升序)
Iterator it = es.iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
String k = (String) entry.getKey();
Object v = entry.getValue();
//空值不傳遞,不參與簽名組串
if (null != v && !"".equals(v)) {
sb.append(k + "=" + v + "&");
sbkey.append(k + "=" + v + "&");
}
}
//System.out.println("字符串:"+sb.toString());
sbkey = sbkey.append("token=" + key);
return StringManage.md5(sbkey.toString());
}
返回就是簽名:
String sign = StringManage.createSign2(parameters, token);
到這裏就結束了