Retrofit2.1.0深入学习@GET【第二章】
2016-09-07 本文已影响29人
Small_Cake
通过上一章的学习,是不是觉得Retrofit用起来很简单,但有的时候需要传递多个参数,这个时候再用@Query就不方便了!我们可以使用@QueryMap直接传递一个Map:
public class WeatherRetrofitGet {
interface WeatherInteface {
String HOST = "http://op.juhe.cn/onebox/weather/";
String JUHE_WEATHER_KEY = "11c39e939a9a32caa5613f9d0e3cf598";
@GET("query")
Call<JSONObject> getJSONObject(@QueryMap Map<String,String> map);
}
public static void doGet(Map<String,String> map,Callback<JSONObject> callback) {
Retrofit build = new Retrofit.Builder().baseUrl(WeatherInteface.HOST).addConverterFactory(JsonConverterFactory.create()).build();
WeatherInteface inteface = build.create(WeatherInteface.class);
inteface.getJSONObject(map).enqueue(callback);
}
}
使用:
Map<String,String> map = new HashMap<>();
map.put("cityname","北京");
map.put("key", "11c39e939a9a32caa5613f9d0e3cf598");
WeatherRetrofitGet.doGet(map, new Callback<JSONObject>() {
@Override
public void onResponse(Call<JSONObject> call, Response<JSONObject> response) {
L.i("map---"+response.body());
tvMsg.setText(response.body().toString());
}
@Override
public void onFailure(Call<JSONObject> call, Throwable t) {
L.e("---"+t.getMessage());
}
});