compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4' 接口package com.example.rxjava.utils; import com.example.rxjava.GoodBeanns; import java.util.Map; import retrofit2.Call; import retrofit2.http.GET; import retrofit2.http.QueryMap; /** * Created by Administrator on 2018/1/18 0018. */ public interface ApiService { //http://120.27.23.105/user/login @GET("user/login") Call<GoodBeanns> getGoods(@QueryMap Map<String,String> map); //做一个bean类 //map中是拼接在后面的 }返回数据的接口package com.example.rxjava.utils; /** * Created by Administrator on 2018/1/18 0018. */ public interface CallBack { void onSuccess(Object o); void onFailed(Throwable t); }请求工具类package com.example.rxjava.utils; import com.example.rxjava.GoodBeanns; import java.util.Map; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; /** * Created by Administrator on 2018/1/18 0018. */ public class RetrofitUtils { private static volatile RetrofitUtils instance; public static RetrofitUtils getInstance(){ if (instance==null){ synchronized (RetrofitUtils.class){ if (instance==null){ instance=new RetrofitUtils(); } } } return instance; } public RetrofitUtils getNews(String baseurl, Map map, final CallBack callBack){ Retrofit retrofit = new Retrofit.Builder() .baseUrl(baseurl) .addConverterFactory(GsonConverterFactory.create()) .build(); ApiService apiService = retrofit.create(ApiService.class); Call<GoodBeanns> call = apiService.getGoods(map); call.enqueue(new Callback<GoodBeanns>() { @Override public void onResponse(Call<GoodBeanns> call, Response<GoodBeanns> response) { GoodBeanns body = response.body(); callBack.onSuccess(body); } @Override public void onFailure(Call<GoodBeanns> call, Throwable t) { callBack.onFailed(t); } }); return null; } } 如何使用Map<String,String> map=new HashMap<>(); map.put("mobile","13269671778"); map.put("password","1234567"); RetrofitUtils retrofitUtils=RetrofitUtils.getInstance().getNews("http://120.27.23.105", map, new CallBack() { @Override public void onSuccess(Object o) { GoodBeanns b= (GoodBeanns) o; Log.d("zzz",b.getMsg()); } @Override public void onFailed(Throwable t) { } });
Retrofit 网络请求框架
最新推荐文章于 2024-08-21 13:45:15 发布