android Retrofit post

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);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

使用方法:只需要传入

  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);
            }

        });
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

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
    );
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

使用:

 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");

            }
        });
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

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

public interface ServiceApi {
    @GET("telematics/v3/movie/")
    Call<Movie> getMovie(
            @QueryMap Map<String, String> map
    );
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

使用的时候提交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");

            }
        });
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

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);
  • 1
  • 2
  • 3
  • 4

@FormUrlEncoded将会自动将请求参数的类型调整为application/x-www-form-urlencoded,假如content传递的参数为Good Luck,那么最后得到的请求体就是

content=Good+Luck
  • 1

@Field注解将每一个请求参数都存放至请求体中,还可以添加encoded参数,该参数为boolean型,具体的用法为

@Field(value = "book", encoded = true) String book
  • 1

@FieldMap 
上述Post请求有4个请求参数,假如说有更多的请求参数,那么通过一个一个的参数传递就显得很麻烦而且容易出错,这个时候就可以用FieldMap

@FormUrlEncoded
 @POST("book/reviews")
 Call<String> addReviews(@FieldMap Map<String, String> fields);
  • 1
  • 2
  • 3

@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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

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);
  • 1
  • 2
  • 3
  • 4
  • 5

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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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
  • 2
  • 3
  • 4
  • 5

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

猜你喜欢

转载自blog.csdn.net/xuyao625010693/article/details/80939720