Retrofit最基本用法

package qianxingzhe.retrofit_learning;

import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.Field;
import retrofit2.http.FieldMap;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Path;
import retrofit2.http.Query;
import retrofit2.http.QueryMap;

/**
 * Created by lunyi.yly on 16/8/7.
 */

public interface GitHubService {
    @GET("repos/{owner}/{repo}/contributors")
    Call<List<Contributor>> listContributor(@Path("owner") String owner, @Path("repo") String repo);

    /**
     * 一个简单的get请求。如: http://102.10.10.132/api/News
     *
     * @return
     */
    @GET("News")
    Call<String> get_01();

    /**
     * URL中有参数。如: http://102.10.10.132/api/News/1
     *
     * @param newsId
     * @return
     */
    @GET("News/{newsId}")
    Call<String> get_02(@Path("newsId") String newsId);

    /**
     * 参数在URL问号之后。如: http://102.10.10.132/api/News?newsId=1
     *
     * @param newsId
     * @return
     */
    @GET("News")
    Call<String> get_03(@Query("newsId") String newsId);

    /**
     * 多个参数在URL问号之后,且个数不确定。如: http://102.10.10.132/api/News?newsId=1&type=类型1...
     *
     * @param map
     * @return
     */
    @GET("News")
    Call<String> get_04(@QueryMap Map<String, String> map);

    /**
     * 需要补全URL,post的数据只有一条reason。如: http://102.10.10.132/api/Comments/1
     *
     * @param newsId
     * @param reason
     * @return
     */
    @FormUrlEncoded
    @POST("Comments/{newsId}")
    Call<String> post_01(@Path("newsId") String newsId, @Field("reason") String reason);

    /**
     * 需要补全URL,问号后加入access_token,post的数据只有一条reason。如:http://102.10.10.132/api/Comments/1?access_token=1234123
     *
     * @param newsId
     * @param access_token
     * @param reason
     * @return
     */
    @FormUrlEncoded
    @POST("Comments/{newsId}")
    Call<String> post_02(@Path("newsId") String newsId, @Query("access_tolen") String access_token, @Field("reason") String reason);

    /**
     * 需要补全URL,问号后加入access_token,post的数据有多条。如:http://102.10.10.132/api/Comments/1?access_token=1234123
     *
     * @param newsId
     * @param access_token
     * @return
     */
    @FormUrlEncoded
    @POST("Comments/{newsId}")
    Call<String> post_03(@Path("newsId") String newsId, @Query("access_tolen") String access_token, @FieldMap Map<String, String> map);

    /**
     * 需要补全URL,问号后加入access_token,post一个body(对象)。如:http://102.10.10.132/api/Comments/1?access_token=1234123
     *
     * @param newsId
     * @param access_token
     * @param contributor
     * @return
     */
    @FormUrlEncoded
    @POST("Comments/{newsId}")
    Call<String> post_04(@Path("newsId") String newsId, @Query("access_tolen") String access_token, @Body Contributor contributor);
}

posted @ 2016-08-07 14:56  yly123  阅读(470)  评论(0编辑  收藏  举报