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'
   
}



0 0
原创粉丝点击