Android: POST and GET requests for Retrofit source resolution

GET request (GET resource)

  • It is used to request access to the resources identified by the URI. The specified resources are parsed by the server and the response content is returned. For Get requests in Retrofit2.0:
Use @ querymap or @ Querry

The full URL of the HTTP request is: www.baidu. COM / API? Name = Allen & sex = male & food = apple

Base URL:

BaseUrl = "www.baidu.com/";

Request method:

public interface MyService {

//First kind
@GET("api? ")
Call<Bean> getInfo(@Query String("name") String name,
                                 @Query("sex") String sex,
                                 @Query("food") String food);

//The second is the same as the first
@GET("api? ")
Call<Bean> getInfo(@QueryMap HashMap<String,String> map);
    }

Network request:

//First kind
Call<Bean> call = MyService .getInfo( "allen","male","apple");
   call.enqueue(new Callback<Bean>() {
    ......
    .........
    ...............

   });

//Second kinds
HashMap<String, String> myHashMap = new HashMap<>();
myHashMap .put ("name", allen);
myHashMap .put ("sex", male);
myHashMap .put ("food", apple);
Call<Bean> call = MyService .getInfo(myHashMap);
   call.enqueue(new Callback<Bean>() {
    ......
    .........
    ...............

   });

POST request (transport entity principal)

  • POST requests are generally divided into two types: transfer entity to the background, form and JSON
Use the form @ FieldMap or @ Field

The full URL of the HTTP request is: www.baidu.com/api
When submitting the form, please note that the form needs to be URL encoded when transferring:
The Parameters are name and Vaule is allen.
Parameters are sex and Vaule is male.
Base URL:

BaseUrl = "www.baidu.com/";

Request method:

public interface MyService {

//First kind
@FormUrlEncoded
@POST("api")
Call<Bean> getPerson(@Field("name") String name,@Field("sex") String sex);

//The second and the first are the same
@FormUrlEncoded
@POST("api")
Call<Bean> getPerson(@FieldMap HashMap<String,String> map);

    }

Network request:

//First kind
Call<Bean> call = MyService .getPerson( "allen","male");
   call.enqueue(new Callback<Bean>() {
    ......
    .........
    ...............

   });

//Second kinds
HashMap<String, String> myHashMap = new HashMap<>();
myHashMap .put ("name", allen);
myHashMap .put ("sex", male);
Call<Bean> call = MyService .getPerson(myHashMap);
   call.enqueue(new Callback<Bean>() {
    ......
    .........
    ...............

   });
Use the form @ Part or @ PartMap directly

PUT request (transfer file)

To be continued

Tags: network JSON

Posted on Fri, 03 Apr 2020 13:23:13 -0700 by Frame