Android Retrofit使用教程(一)
来源:互联网 发布:navicat for mysql语法 编辑:程序博客网 时间:2024/05/19 09:13
入门篇
保证基本使用 能看懂 且没封装
前期准备
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呢 ,最后我用了一个完整的多级页面进行测试(”http://op.juhe.cn/onebox/weather/query“;) ,正常请求;我遇到的错误可能是自家后台数据的问题了 ;
- Android Retrofit使用教程(一)
- Retrofit使用教程(一)
- Retrofit使用教程(一)
- Retrofit使用教程(一)
- Android Retrofit使用(一)
- Retrofit使用教程(一)- Retrofit入门详解
- Retrofit使用教程(一)- Retrofit入门详解
- Retrofit的使用教程(一)
- Retrofit的使用教程(一) 1.9
- Android Retrofit 2.0(二)使用教程OkHttp3 + Gson + RxJava
- 【Retrofit 】Retrofit 基本使用教程
- Android retrofit历程(一)
- android retrofit 示例(一)
- Retrofit 2 使用教程
- Retrofit 基本使用教程
- Retrofit使用教程(二)
- Retrofit 基本使用教程
- Retrofit使用教程(二)
- SpringMVC结合Sitemesh和ztree出现的Cannot read property 'init' of undefined
- View onMeasure(int widthMeasureSpec, int heightMeasureSpec)
- 软键盘的显示和隐藏
- Unity 初级保龄球游戏
- 关于AsyncTask的执行序理解
- Android Retrofit使用教程(一)
- hdu1811Rank of Tetris
- Nginx出现413 Request Entity Too Large错误解决方法
- C++15道基础算法题---(2)排列组合
- 腾讯8.3
- Java中byte基础类型的特殊性
- 我所理解的REST API
- MFC播放wav音频文件
- css文本