Retrofit學(xué)習(xí)(三)-普通post請求
Retrofit學(xué)習(xí)(一)-集成
http://www.reibang.com/p/86e5cddcc753
Retrofit學(xué)習(xí)(二)-get請求
http://www.reibang.com/p/cdde02a0777c
Retrofit學(xué)習(xí)(三)-普通post請求
http://www.reibang.com/p/7f252d10fd41
Retrofit學(xué)習(xí)(四)-下載文件
http://www.reibang.com/p/f61645a770ae
Retrofit學(xué)習(xí)(五)-文件上傳
http://www.reibang.com/p/ca0cb8640c8f
Post表單提交-單個參數(shù) @Field
- 接口
/** 表單提交要加 @FormUrlEncoded
* 登錄
* @param username 用戶名
* @param password 密碼
* @param deviceId 設(shè)置ID
* @param platform 平臺這里是Android
* @param verId 版本號
* @return
*/
@FormUrlEncoded
@POST("login")
Call<String> login(@Field("loginName") String username,
@Field("password") String password,
@Field("deviceId") String deviceId,
@Field("platform") String platform,
@Field("verId") String verId);
- 調(diào)用
private void login(){
/**
* 初始化
*/
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
//生成對象的Service
LoginService loginService = retrofit.create(LoginService.class);
//調(diào)用方法得到Call
Call<String> loginCall = loginService.login("home123",
"123456",
getDeviceId(),
PLATFORM,
VERSION_ID);
//異步執(zhí)行
loginCall.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
L.d("vivi",response.message()+" "+response.body());
}
@Override
public void onFailure(Call<String> call, Throwable t) {
L.d("vivi",t.getMessage());
L.d("vivi",t.toString());
}
});
}
/**
* 獲取設(shè)備號
* @return
*/
private String getDeviceId(){
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String deviceId = tm.getDeviceId();
L.d("vivi",deviceId);
return deviceId;
}
- 權(quán)限
<!--讀取手機(jī)狀態(tài)的權(quán)限-->
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
- 結(jié)果
OK {"msg":"OK","result":{"accessToken":"A0BdgCrMG22CwfSKrlsEPAs3ZAozND","securiyCode":"5jUduYno","time":null,"userId":"402882823a9d5e0d013a9dabe3680002","userName":"home","verId":"V1.0"},"status":1,"verId":"1.0"}
Post表單提交-多個參數(shù)-@FieldMap
/**
* 表單提交要加 @FormUrlEncoded
* Post使用map多參數(shù)
* @param params
* @return
*/
@FormUrlEncoded
@POST("login")
Call<LoginBean> login2(@FieldMap Map<String,String>params);
- 調(diào)用
/**
* 初始化
*/
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
Map<String, String> hashMap = new HashMap<String, String>();
hashMap.put("loginName", "home123");
hashMap.put("password", "123456");
hashMap.put("deviceId", getDeviceId());
hashMap.put("platform", PLATFORM);
hashMap.put("verId", VERSION_ID);
//生成對象的Service
LoginService loginService = retrofit.create(LoginService.class);
//調(diào)用方法得到Call
Call<LoginBean> loginCall = loginService.login2(hashMap);
//異步執(zhí)行
loginCall.enqueue(new Callback<LoginBean>() {
@Override
public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
L.d("vivi",response.message()+" "+response.body().toString());
}
@Override
public void onFailure(Call<LoginBean> call, Throwable t) {
L.d("vivi",t.getMessage());
L.d("vivi",t.toString());
}
});
結(jié)果與第一個一樣峻村,這樣就拿到token
使用@HeaderMap過濾請求 動態(tài)添加請求動
@Headers()
這個是寫在方法上的
如果請求頭是固定的袖牙,就可以使用這個
@Header與@HeaderMap作為參數(shù)使用的
請求頭不固定,要動態(tài)添加 就使用瞬项,2個用法就和@Field 與@FieldMap一樣
- 接口
@POST("p/userCenter")
Call<String> getUserCenter(@HeaderMap Map<String,String> headers);
- 調(diào)用
private void getUserInfo(){
/**
* 初始化
*/
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
//生成對象的Service
LoginService loginService = retrofit.create(LoginService.class);
/* SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = dateFormat.format(new Date());*/
/**
* 這里測試參數(shù)寫死了龟梦,實(shí)際可以改變诺祸,
*/
Map<String, String> headers = new HashMap<String, String>();
headers.put("userId", "402882823a9d5e0d013a9dabe3680002");
headers.put("userName", "home");
headers.put("accessToken", "A0BdgCrMG22CwfSKrlsEPAs3ZAozND");
//當(dāng)前時間旁振,因為后臺加密揣钦,原因?qū)懰?
headers.put("time", "2016-10-27 13:28:07");
headers.put("verId","V1.0");
//這個是加載后的數(shù)據(jù)后臺接收后雳灾,同樣有一個比對
String validateCode = "674BA63B66D0D4A1E6C9EED16CE193C7";
headers.put("code", validateCode);
Call<String> userCall = loginService.getUserCenter(headers);
userCall.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
L.d("vivi",response.message()+" "+response.body());
}
@Override
public void onFailure(Call<String> call, Throwable t) {
L.d("vivi",t.getMessage());
L.d("vivi",t.toString());
}
});
}
- 結(jié)果
OK {"msg":"OK","result":{"availablePredeposit":28,"favoriteLength":2,"messageCount":1,"nickName":"home123","portraitPic":"2016/10/27/92f2272d-cf41-4f22-bf46-905575197441.jpg","score":602,"subCountsDtoList":[{"status":"UNPAY","subCounts":15},{"status":"PADYED","subCounts":1},{"status":"SUCCESS","subCounts":1},{"status":"CLOSE","subCounts":3}],"userId":"402882823a9d5e0d013a9dabe3680002","userMobile":"13800138000","userName":"home"},"status":1,"verId":"1.0"}
- 如果傳過去是一個空的map就請求不到數(shù)據(jù)
OK {"msg":"401 user validate failed" ,"result":null,"status":"401","verId":"1.0"}
@Headers()
@Header()
@HeaderMap
基本上是用于過濾請求的