美文网首页
Retrofit2.1.0深入学习@POST【第四章】

Retrofit2.1.0深入学习@POST【第四章】

作者: Small_Cake | 来源:发表于2016-09-24 13:58 被阅读67次

POST请求和GET请求差不多,主要它多了许多的参数,于是我们用的最多的也是通过Map把值传递进去,这个和GET直接在后面追加参数的方式不同!

public class WeatherRetrofitPost {    
    interface WeatherInteface {        
        String HOST = "http://op.juhe.cn/onebox/weather/";        
        @POST("query")        
        Call<JSONObject> postJSONObject(@QueryMap Map<String,String> map);    
    }    
    public static void doPost(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.postJSONObject(map).enqueue(callback);    
    }
}

然后在主页请求

private void doPost() {    
      Map<String,String> map = new HashMap<>();    
      map.put("cityname","北京");    
      map.put("key", "11c39e939a9a32caa5613f9d0e3cf598");    
      WeatherRetrofitPost.doPost(map, new Callback<JSONObject>() {        
          @Override        
          public void onResponse(Call<JSONObject> call,Response<JSONObject> response) {            
              tvMsg.setText(response.body().toString());        
          }        
          @Override        
          public void onFailure(Call<JSONObject> call, Throwable t) {}    
      });
}
Paste_Image.png

相关文章

网友评论

      本文标题:Retrofit2.1.0深入学习@POST【第四章】

      本文链接:https://www.haomeiwen.com/subject/xkyxyttx.html