1. Four Ways of Request:

GET

POST

DELETE

PUT

Retrofit configures different network requests through annotations. It mainly involves the usage of @path, @query, @querymap, @body, @field, etc.

Suppose your BASE_URL = “http://192.168.0.1/”

1, GET request:


   @GET("weather")
   Observable<WeatherEntity> getWeather(@Query("city") String city);
Copy the code

(2) the case 2: @ Path request parameters directly with under the request Path: http://192.168.0.1/weather/ in Beijing

    @GET("weather/{city_name}")
    Observable<Object> getWeather(@Path("city_name") String city_name);
Copy the code

(3) case 3: @ Path and @ QueryMap combination This kind of situation with less: http://192.168.0.1/weather/ in Beijing? user_id=1&user_name=jojo

    @GET("weather/{city_name}")
    Observable<Object> getWeather(@Path("city_name")String city_name, @QueryMap Map<String, String> queryParams);
Copy the code
    HashMap<String, String> queryParams= new HashMap<>();
    hashMap.put("user_id"."1");
    hashMap.put("user_name"."jojo");
Copy the code


2. POST request:








@formurlencoded // When using @field, remember to add @formurlencoded @post ("comment")
    void doComments(@Field("comment_id")String comment_id, @Field("content")String content, @Field("user_id") String user_id);
Copy the code

@fieldMap

    @FormUrlEncoded
    @POST("comment")
    void doComments(@FieldMap Map<String, String> paramsMap );
Copy the code

Submit as a form with key-value pairs:

        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("comment_id"."1");
        hashMap.put("content"."I'm a critic.");
        hashMap.put("user_id"."1001");
Copy the code

@body

    @POST("comment")
    void doComments(@Body Object reqBean);
Copy the code
   @POST("comment")
    void doComments(@Body List<Object> requestList);
Copy the code

(2) the case 2: Retrofit file upload: http://192.168.0.1/upload/

/** * upload file */ @post ("upload/")
    Observable<Object> uploadFile(@Body RequestBody requestBody);
Copy the code

A file upload is passed in as a RequestBody type. Here’s how to build a RequestBody:

File file = new File(mFilePath); Body RequestBody RequestBody = new MultipartBody.builder ().setType(multipartbody.form) .addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), file))
                .build();
Copy the code


1, PUT request:


    @PUT("comment/{comment_id}")
    void comment(@Path("comment_id") String comment_id);
Copy the code

(2) the case 2: http://192.168.0.1/comment/88? user_id=1001

    @PUT("comment/{comment_id}")
    void comment(@Path("comment_id") String comment_id @Query("user_id") String user_id);
Copy the code

(3) case 3: http://192.168.0.1/comment/88? User_id =1001 + body: {“content”:” I am a comment “, “type”:”1″}

Application scenarios of this type of request: It is suitable for body which needs to pass in multiple request parameters. In this way, parameter fields of multiple requests can be encapsulated into an entity, so that multiple @Filed are not required.

public class RequestBean {
    public String content;
    public String type; // There may actually be multiple request fields}Copy the code
    RequestBean  requestBean = new RequestBean();
    requestBean .content = "I'm a critic.";
    requestBean .type = "1";
Copy the code
    @PUT("comment/{comment_id}")
    void comment(@Path("comment_id") String comment_id @Query("user_id") String user_id @Body RequestBean reqBean);
Copy the code


1, DELETE request:


    @DELETE("comment/{comment_id}")
    void comment(@Path("comment_id") String comment_id);
Copy the code

Other possible DELETE requests are configured similarly, without further examples. It is important to understand the role and usage of these common request parameter annotations. @path: when the value of the request parameter is directly followed by the URL, @path is configured to @query: to indicate the Query parameter, as? @query @queryMap: a Query parameter that is directly passed to multiple key-value pairs in the form of map. @field: this parameter is used for form fields in POST requests. Each @Field corresponds to a pair of key-value pairs. @fieldMap: Pass in multiple key-value pairs as a map, as the body argument. @body: equivalent to multiple @fields, submitted as objects

Filed and FieldMap need to be used together with FormUrlEncoded, otherwise exceptions will be thrown!