入门篇
保证基本使用 能看懂 且没封装
前期准备
app的gradle文件里
compile ‘com.squareup.retrofit:retrofit:2.0.0-beta2’
compile ‘com.squareup.okhttp:okhttp:2.5.0’// 网络请求 同时依赖
compile ‘com.squareup.okio:okio:1.6.0’
compile ‘com.google.code.gson:gson:2.4’
compile ‘com.squareup.retrofit:converter-gson:2.0.0-beta2’//json解析为DAO模型
不解析测试:
public interface Login {
// @GET("/user/{username}")
// Call getUser(@Path("username") String username);
//http://172.16.2.81/api/applogin.php?username=xxx&password=xxx(这是完整的url供格式上的参考)
@GET("username={username}&password={password}")
// Call login(@Path("username") String username, @Path("password") String password);
Call login(@Path("username") String username, @Path("password") String password);
@GET("api/applogin.php")
Call login2(@Query("username") String username, @Query("password") String password);
}
复制代码
Activity中:
public class RetrofitActivity extends BaseActivity {
private Button retrofitReq;
private String ctmLogin = "http://172.16.3.8:8092/";// 请换成自己能请求下来的url
private String param_user = "username";
private String param_pass = "password";
private String userename;
private String password;
Login loginservice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_retrofit);
retrofitReq = fView(R.id.retrofitReq);
showSnack(retrofitReq, "Retrofit体验区");
// (1)创建Retrofit实例
Retrofit req = new Retrofit.Builder().baseUrl(ctmLogin).
// addConverterFactory(GsonConverterFactory.create()).
build();
// (2)定义Endpoints,实现了转换HTTP API为Java接口
// @GET("user/list")
// (3)Accessing the API
loginservice = req.create(Login.class);
userename = "yihaibin";
password = "oacrmtest";
}
public void send(View v) {
final Call call = loginservice.login2(userename, password);
new Thread(new Runnable() {
@Override
public void run() {
try {
Response responseBodyResponse = call.execute();
ResponseBody body = responseBodyResponse.body();
DebugLogUtil.getInstance().Info("" + body.string());
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}
复制代码
添加GSon解析
将 addConverterFactory(GsonConverterFactory.create()). 注释放开
然后再send()函数中替换为以下内容
final Call callLogin = loginservice.login(userename, password);
callLogin.enqueue(new Callback() {
@Override
public void onResponse(Response response, Retrofit retrofit) {
User user = response.body();
DebugLogUtil.getInstance().Info("code=" + response.code());
DebugLogUtil.getInstance().Info("response.message()=" + response.message());
DebugLogUtil.getInstance().Info("response.toString()=" + response.toString());
}
@Override
public void onFailure(Throwable t) {
DebugLogUtil.getInstance().Info("onFailure" + t);
}
});
复制代码
但是,如上部分代码,在请求成功的回调中,得到的返回值跟预期不一样 只有字母 没有汉字“登陆成功”(是后台写的);
反省了,属于我粗心,model类写错了 ,好长时间没加接口到项目中,都忘了怎么弄了 才想起来;
final Call callLogin = loginservice.login(userename, password);
callLogin.enqueue(new Callback() {
@Override
public void onResponse(Response response, Retrofit retrofit) {
User user = response.body();
DebugLogUtil.getInstance().Info("code=" + user.getCode());
DebugLogUtil.getInstance().Info("response.message()=" + user.getDatas().getMessage());
}
@Override
public void onFailure(Throwable t) {
DebugLogUtil.getInstance().Info("onFailure=" + t);
}
});
复制代码
javabean
package com.czh.khjingying.model;
/**
* 登录返回信息
* Created by LiJianfei on 2016/8/2.
*/
public class User {
/**
* code : 200
* datas : {"key":"e????9a","message":"登陆成功"}
*/
private String code;
/**
* key : ea7?????9a66f678f9a
* message : 登陆成功
*/
private DatasBean datas;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public DatasBean getDatas() {
return datas;
}
public void setDatas(DatasBean datas) {
this.datas = datas;
}
public static class DatasBean {
private String key;
private String message;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
}
复制代码
出错记录
1 public final static String ctmLogin = “http://172.16.3.8:8092/api“;
在interface中:
@GET(“/applogin.php”)// 不用问号
Call login(@Query(“username”) String username, @Query(“password”) String password);
会出错:
user空指针;如果按照我前面教程里拼接方式 不会出错(方式不限于引用还是直接赋值,关键在于,拼在interface中的部分)。如果之前的interface内容不能正常请求的话
就改成这样@GET("/api/applogin.php")// 不用问号
Call login(@Query("username") String username, @Query("password") String password);
复制代码
tip:开始我以为是二级页面不能作为baseurl呢 ,最后我用了一个完整的多级页面进行测试(”op.juhe.cn/onebox/weat…“;) ,正常请求;我遇到的错误可能是自家后台数据的问题了 ;