url请求示例
http://gank.io/api/data/福利/{pageCount}/{pageIndex}
http://gank.io/api/data/福利/5/1(5和1代表分页中的参数)
public interface Api {
//http://gank.io/api/data/福利/5/1
@GET("api/data/福利/{pageCount}/{pageIndex}")
Call getData(@Path("pageCount") int pageCount,
@Path("pageIndex") int pageIndex);
}
使用方法:只需要传入
Retrofit retrofit=new Retrofit.Builder()
.baseUrl("http://gank.io/")
.addConverterFactory(GsonConverterFactory.create())
.build();
Api api =retrofit.create(Api.class);
Call call=api.getData(5,pages);
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
arrayList.addAll(response.body().results);
adapter.notifyDataSetChanged();
Log.i("aaaa", arrayList.size() + "");
refreshLayout.setRefreshing(false);
}
@Override
public void onFailure(Call call, Throwable t) {
refreshLayout.setRefreshing(false);
}
});
url示例
http://api.map.baidu.com/telematics/v3/movie
?qt=hot_movie
&location=北京
&output=json
&ak=A72e372de05e63c8740b2622d0ed8ab1
方法一:每一个字段拼接到后面
@GET("telematics/v3/movie/{qt}/{location}/{output}/{ak}")
Call getMovie(
@Query("qt") String qt,
@Query("location") String location,
@Query("output") String output,
@Query("ak") String ak
) ;
使用:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://api.map.baidu.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
ServiceApi api = retrofit.create(ServiceApi.class);
Call call = api.getMovie("hot_movie","北京","json","A72e372de05e63c8740b2622d0ed8ab1");
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
Movie movie=response.body();
String date=movie.date;
String error=movie.error;
Movie.MoiveDeatil result = movie.result;
list=result.movie;
Log.i("date", date);
Log.i("error", error);
Log.i("result", list+"");
adapter=new MyRecyclerViewAdapter(MainActivity.this,list);
mRecyclerView.setAdapter(adapter);
}
@Override
public void onFailure(Call call, Throwable t) {
Log.i(TAG, "");
text.setText("onFailure");
}
});
第二种方法,提交一个map
接口配置
public interface ServiceApi {
@GET("telematics/v3/movie/")
Call getMovie(
@QueryMap Map map
);
}
使用的时候提交map:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://api.map.baidu.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
ServiceApi api = retrofit.create(ServiceApi.class);
Map map = new HashMap();
map.put("qt", "hot_movie");
map.put("location", "北京");
map.put("output", "json");
map.put("ak", "A72e372de05e63c8740b2622d0ed8ab1");
Call call = api.getMovie(map);
// Call call = api.getMovie("hot_movie","北京","json","A72e372de05e63c8740b2622d0ed8ab1");
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
Movie movie=response.body();
String date=movie.date;
String error=movie.error;
Movie.MoiveDeatil result = movie.result;
list=result.movie;
Log.i("date", date);
Log.i("error", error);
Log.i("result", list+"");
adapter=new MyRecyclerViewAdapter(MainActivity.this,list);
mRecyclerView.setAdapter(adapter);
}
@Override
public void onFailure(Call call, Throwable t) {
Log.i(TAG, "");
text.setText("onFailure");
}
});
post请求是将请求参数放在请求体中,而不是拼接到url后面,使用@field
@FormUrlEncoded
@POST("book/reviews")
Call addReviews(@Field("book") String bookId, @Field("title") String title,
@Field("content") String content, @Field("rating") String rating) ;
@FormUrlEncoded将会自动将请求参数的类型调整为application/x-www-form-urlencoded,假如content传递的参数为Good Luck,那么最后得到的请求体就是
content=Good+Luck
@Field注解将每一个请求参数都存放至请求体中,还可以添加encoded参数,该参数为boolean型,具体的用法为
@Field(value = "book", encoded = true) String book
@FieldMap
上述Post请求有4个请求参数,假如说有更多的请求参数,那么通过一个一个的参数传递就显得很麻烦而且容易出错,这个时候就可以用FieldMap
@FormUrlEncoded
@POST("book/reviews")
Call addReviews(@FieldMap Map fields) ;
@Body
如果Post请求参数有多个,那么统一封装到类中应该会更好,这样维护起来会非常方便
@FormUrlEncoded
@POST("book/reviews")
Call addReviews(@Body Reviews reviews);
public class Reviews {
public String book;
public String title;
public String content;
public String rating;
}
1、需要补全URL,post的数据只有一条reason
http://102.10.10.132/api/Comments/1
http://102.10.10.132/api/Comments/{newsId}
@FormUrlEncoded
@POST("Comments/{newsId}")
Call reportComment(
@Path("newsId") String commentId,
@Field("reason") String reason) ;
2、需要补全URL,问号后加入access_token,post的数据只有一条reason
http://102.10.10.132/api/Comments/1?access_token=1234123
http://102.10.10.132/api/Comments/{newsId}?access_token={access_token}
@FormUrlEncoded
@POST("Comments/{newsId}")
Call reportComment(
@Path("newsId") String commentId,
@Query("access_token") String access_token,
@Field("reason") String reason) ;
3、需要补全URL,问号后加入access_token,post一个body(对象)
http://102.10.10.132/api/Comments/1?access_token=1234123
http://102.10.10.132/api/Comments/{newsId}?access_token={access_token}
@POST("Comments/{newsId}")
Call<Comment> reportComment(
@Path("newsId") String commentId,
@Query("access_token") String access_token,
@Body CommentBean bean);
1、url中没有?的时候用Path
http://gank.io/api/data/福利/5/1
2、url中有?的时候用Query
http://api.map.baidu.com/telematics/v3/movie
?qt=hot_movie
&location=北京
&output=json
&ak=A72e372de05e63c8740b2622d0ed8ab1
参考博客
http://blog.csdn.net/jdsjlzx/article/details/51607867
http://blog.csdn.net/duanyy1990/article/details/52139294