Retrofit提交参数

Retrofit提交参数

1、Get请求
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<DataInfo> 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<DataInfo> call=api.getData(5,pages);
        call.enqueue(new Callback<DataInfo>() {
            @Override
            public void onResponse(Call<DataInfo> call, Response<DataInfo> response) {

                arrayList.addAll(response.body().results);
                adapter.notifyDataSetChanged();
                Log.i("aaaa", arrayList.size() + "");
                refreshLayout.setRefreshing(false);

            }

            @Override
            public void onFailure(Call<DataInfo> call, Throwable t) {
                refreshLayout.setRefreshing(false);
            }

        });



2、提交多个查询参数
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<Movie> 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<Movie> call = api.getMovie("hot_movie","北京","json","A72e372de05e63c8740b2622d0ed8ab1");
        call.enqueue(new Callback<Movie>() {
            @Override
            public void onResponse(Call<Movie> call, Response<Movie> 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<Movie> call, Throwable t) {
                Log.i(TAG, "");
                text.setText("onFailure");

            }
        });



第二种方法,提交一个map 
接口配置

public interface ServiceApi {
    @GET("telematics/v3/movie/")
    Call<Movie> getMovie(
            @QueryMap Map<String, String> 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<Movie> call = api.getMovie(map);
       // Call<Movie> call = api.getMovie("hot_movie","北京","json","A72e372de05e63c8740b2622d0ed8ab1");
        call.enqueue(new Callback<Movie>() {
            @Override
            public void onResponse(Call<Movie> call, Response<Movie> 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<Movie> call, Throwable t) {
                Log.i(TAG, "");
                text.setText("onFailure");

            }
        });



3、Post请求
post请求是将请求参数放在请求体中,而不是拼接到url后面,使用@field

@FormUrlEncoded
@POST("book/reviews")
Call<String> 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<String> addReviews(@FieldMap Map<String, String> fields);

@Body 
如果Post请求参数有多个,那么统一封装到类中应该会更好,这样维护起来会非常方便

@FormUrlEncoded
@POST("book/reviews")
Call<String> 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<Comment> 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<Comment> 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);

4、@Path和Query的区别
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
 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值