之前没用过retrofit,最近项目里引入了这个流行库,学习用吧,按照例子(sample
中的RequestBuilderTest.java),就拿公司的登录接口来测试。
首先,定义个request接口:
public interface LoginRequest {
@POST("logon/login")
Call<ResponseBody> getString();//ResponseBody代表直接获得返回的body
}
然后使用接口:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Config.baseUrl)
.build();
LoginRequest requestServices = retrofit.create(LoginRequest.class);
final Call<ResponseBody> call = requestServices.getString();
// Fetch and print a list of the contributors to the library.
new Thread(new Runnable() {
@Override
public void run() {
ResponseBody responseBody = null;
try {
responseBody = call.execute().body();
Log.e("=============", "responseBody"+responseBody.string().toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
运行,返回:
responseBody{“code”:500,“msg”:“com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input\n at [Source: org.apache.catalina.connector.CoyoteInputStream@76412093; line: 1, column: 1]”}
接下来添加请求的header和body参数。修改后如下:
public interface LoginRequest {
@FormUrlEncoded
@Headers("Content-Type:application/json")
@POST("logon/login")
Call<ResponseBody> login(@Field("uid") String name,
@Field("pwd") String pwd,
@Field("rid") String rid,
@Field("clientId") String clientId,
@Field("forAccessToken") boolean forAccessToken);
}
同时,请求的地方改成:
final Call<ResponseBody> call = requestServices.login("13588745152", stringToMD5("123456"), "doctor", "2865", true);
运行,发现报
responseBody{“code”:500,“msg”:“com.fasterxml.jackson.core.JsonParseException: Unrecognized token ‘uid’: was expecting (‘true’, ‘false’ or ‘null’)\n at [Source: org.apache.catalina.connector.CoyoteInputStream@26c8f101; line: 1, column: 5]”}
一脸懵逼,只好去打印它发出去的请求内容看看。加日志拦截器:
private OkHttpClient okhttpclient() {
if (mOkHttpClient == null) {
HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor(new HttpLogger());
logInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
mOkHttpClient = new OkHttpClient.Builder()
.connectTimeout(15, TimeUnit.SECONDS)
.addNetworkInterceptor(logInterceptor)
.build();
}
return mOkHttpClient;
}
打印出的结果如下:
08-30 15:56:11.747 5062-5115/com.example.dell.retrofittest D/HttpLogInfo: --> POST http://ngaribata.ngarihealth.com:8480/ehealth-base-devtest/logon/login http/1.1
08-30 15:56:11.747 5062-5115/com.example.dell.retrofittest D/HttpLogInfo: Content-Type: application/json
08-30 15:56:11.747 5062-5115/com.example.dell.retrofittest D/HttpLogInfo: Content-Length: 97
08-30 15:56:11.747 5062-5115/com.example.dell.retrofittest D/HttpLogInfo: charset: UTF-8
08-30 15:56:11.747 5062-5115/com.example.dell.retrofittest D/HttpLogInfo: Host: ngaribata.ngarihealth.com:8480
08-30 15:56:11.747 5062-5115/com.example.dell.retrofittest D/HttpLogInfo: Connection: Keep-Alive
08-30 15:56:11.747 5062-5115/com.example.dell.retrofittest D/HttpLogInfo: Accept-Encoding: gzip
08-30 15:56:11.748 5062-5115/com.example.dell.retrofittest D/HttpLogInfo: User-Agent: okhttp/3.8.0
08-30 15:56:11.748 5062-5115/com.example.dell.retrofittest D/HttpLogInfo: uid=13588745152&pwd=e10adc3949ba59abbe56e057f20f883e&rid=doctor&clientId=2865&forAccessToken=true
08-30 15:56:11.749 5062-5115/com.example.dell.retrofittest D/HttpLogInfo: --> END POST (97-byte body)
参数不对。重新写另外一种:
@Headers({"Content-Type:application/json"
,"charset:UTF-8"})
@POST("logon/login")
Call<ResponseBody> login1(@Body UserRequestBean body);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Config.baseUrl)
.client(mOkHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
LoginRequest requestServices = retrofit.create(LoginRequest.class);
//final Call<ResponseBody> call = requestServices.login("\"13588745152\"", stringToMD5("123456"), "doctor", "2865", true);
// Fetch and print a list of the contributors to the library.
UserRequestBean bean = new UserRequestBean();
bean.setUid("13588745152");
bean.setPwd(stringToMD5("123456"));
bean.setRid( "doctor");
bean.setClientId("2865");
bean.setforAccessToken(true);
final Call<ResponseBody> call = requestServices.login1(bean);
返回正确值。