// //http:// v.juhe.cn/ toutiao/index ?type=top&key=097060266650f67b2cebd2a06aded587
// //协议 ip地址+端口号 项目路径 请求参数
// @GET("toutiao/index")
// Call<XinWen> getNewsInfo(@Query("type")String leixing,@Query("key")String key);
//
// @GET("toutiao/index")
// Call<XinWen> getNewsInfo(@QueryMap Map<String,String> map);
//
// @FormUrlEncoded//表单提交
// @POST("toutiao/index")
// Call<XinWen> getNewsInfo_Post(@Field("type") String type, @Field("key") String key);
//
// @FormUrlEncoded//表单提交
// @POST("toutiao/index")
// Call<XinWen> getNewsInfo_Post(@FieldMap Map<String,String> map);
Retrofit build = new Retrofit.Builder().baseUrl("http://v.juhe.cn/").addConverterFactory(GsonConverterFactory.create(new Gson())).build();
Tou tou = build.create(Tou.class);
//模式一
// Call<XinWen> newsInfo = tou.getNewsInfo_Post("top", "f7c582ae70a71ac819c00f2d5b54ab48");
//模式二
HashMap<String, String> map = new HashMap<>();
map.put("type","top");
map.put("key","f7c582ae70a71ac819c00f2d5b54ab48");
Call<XinWen> newsInfo = tou.getNewsInfo_Post(map);
newsInfo.enqueue(new Callback<XinWen>() {
@Override
public void onResponse(Call<XinWen> call, Response<XinWen> response) {
XinWen body = response.body();
String title = body.getResult().getData().get(0).getTitle();
Log.e("MainActivity", title);
}
@Override
public void onFailure(Call<XinWen> call, Throwable t) {
}
});
Retrofit
最新推荐文章于 2021-09-04 23:48:34 发布