Retrofit使用
来源:互联网 发布:腾讯红包数据 编辑:程序博客网 时间:2024/06/04 17:57
//这段代码放到 业务层公共操作的封装
protected static ResponseInfoAPI responseInfoAPI;
public Presenter() {
if (responseInfoAPI == null) {
//网络访问//第一步,创建Builder,指定baseUrl和数据解析工具
Retrofit.Builder reBuilder = new Retrofit.Builder();
reBuilder.baseUrl(Constant.LOGIN); //指定要连接的网络路径
reBuilder.addConverterFactory(GsonConverterFactory.create()); //指定要使用的解析方式
//第二步创建 Retrofit
Retrofit retrofit = reBuilder.build();
//第三步 指定请求方式(get或post)和参数,通过以接口的形式指定
//第三步 通过接口ResponseInfoAPI
//第四步 将 Retrofit和第三步的联网参数联系起来
responseInfoAPI = retrofit.create(ResponseInfoAPI.class);
}
}
public interface ResponseInfoAPI {
//设置请求方式
@GET(Constant.APISERVER)
Call<ResponseInfo> login(@Query("username") String userName, @Query("password") String passWord);
//首页
@GET(Constant.HOME)
Call<ResponseInfo> home();
//商品
@GET(Constant.RECOMMENDINFO)
Call<ResponseInfo> reCommendInfo(@Query("sellerId") int sellerId);
/*
phone 电话
type 登陆类型(必须)普通登陆:1;短信验证:2;三方登录:3
*/
//手机验证码登录
@GET(Constant.SMSLOGIN)
Call<ResponseInfo> smsLogin(@Query("phone") String phone,@Query("type") int type);
//地址
@GET(Constant.ADDRESS)
Call<ResponseInfo> address(@Query("userId") int userId);
//提交订单到服务器
@FormUrlEncoded
@POST(Constant.ORADER)
Call<ResponseInfo> orader(@Field("orderOverview") String s);
//支付
@GET(Constant.PAY)
Call<ResponseInfo> pay(@Query("orderId") String oraderNumber);
@GET(Constant.ORADER)
Call<ResponseInfo> orderList(@Query("userId") int userid);
}
public class Constant {
public static final String LOGIN = "http://192.168.0.103:8080/";
//登录
public static final String APISERVER="TakeoutService/login";
//首页
public static final String HOME="TakeoutService/home";
//商品 http://localhost:8080/TakeoutService/goods?sellerId=1
public static final String RECOMMENDINFO = "TakeoutService/goods";
//sms登录
public static final String SMSLOGIN="TakeoutService/login";
public static final int SMSTYPE=2; //验证码登录
//地址 http://localhost:8080/TakeoutService/address?userId=1794
public static final String ADDRESS="TakeoutService/address";
//提交订单
public static final String ORADER = "TakeoutService/order";
//支付
public static final String PAY = "TakeoutService/pay";
//添加Retrofit依赖:
compile 'com.squareup.retrofit2:retrofit:2.1.0'
// 使用Gson进行数据解析
compile 'com.google.code.gson:gson:2.2.4'
// 将Retorfit与Gson关联
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
}
- Retrofit使用
- Retrofit使用
- Retrofit使用
- Retrofit使用
- Retrofit使用
- Retrofit使用
- Retrofit使用
- 【Retrofit 】Retrofit 基本使用教程
- 《Retrofit系列》Retrofit的基本使用
- 【Android】Retrofit的使用(1)-Retrofit介绍
- Retrofit框架使用笔记
- Retrofit 2 使用教程
- retrofit使用注意事项
- Retrofit 的使用
- retrofit 的使用。
- 使用Retrofit请求API
- Retrofit 基本使用教程
- Retrofit 的使用简介
- 关于导入“新浪微盘”工程错误
- Java集合——HashMap和Hashtable应用实例
- RCNN系列学习笔记(5):You Only Look Once(YOLO):Unified, Real-Time Object Detection
- DefaultAnnotationHandlerMapping
- 234. Palindrome Linked List
- Retrofit使用
- LeetCode中338Counting Bits的题解
- JavaSE_40th_自动装箱、拆箱
- 怎样把cookic保存到文件里
- Git远程仓库提交操作流程
- 如何在Android中集成支付宝
- 微信公众号【沈万三爱搬砖】的自述
- ElasticSearch 的 聚合(Aggregations)
- Try-with-resources (TWR)