Retrofit的简单使用

来源:互联网 发布:手机系统检测软件 编辑:程序博客网 时间:2024/06/01 10:28
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'//Retrofit1所需要的包compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'//ConverterFactory的Gson依赖包
package com.example.retrofitdemo;import retrofit2.Call;import retrofit2.http.GET;import retrofit2.http.POST;import retrofit2.http.Query;/** * Created by xsj on 2017/12/1. */public interface demo {    //get请求    @GET("ad/getAd")    Call<ShopBean> getCall();    @POST("user/login")    Call<UserBean> postCall(@Query("mobile") String mo, @Query("password") String dd);

}

Retrofit build = new Retrofit.Builder()        .baseUrl("http://120.27.23.105/")        .addConverterFactory(GsonConverterFactory.create())        .build();demo demo = build.create(demo.class);Call<UserBean> userBeanCall = demo.postCall("15340986701", "123456");


package com.example.wuxiruionetext.Utils;import android.content.Context;import android.net.ConnectivityManager;import android.net.NetworkInfo;/** * Created by xsj on 2017/12/2. */public class NetUils {    private static volatile NetUils instance;    private Context context;    private NetUils(Context context) {        this.context = context;    }    public static NetUils getInstance(Context context) {        if (instance == null) {            synchronized (NetUils.class) {                if (instance == null) {                    instance = new NetUils(context);                }            }        }        return instance;    }    public int getNetype() {        int netType = -1;        ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();        //无网络        if (networkInfo == null) {            return netType;        }        int nType = networkInfo.getType();        //手机网络        if (nType == ConnectivityManager.TYPE_MOBILE) {            netType = 2;        } else if (nType == ConnectivityManager.TYPE_WIFI) {//wifi网络            netType = 1;        }        //返回        return netType;    }}