Retrofit post request with parameters

后端 未结 3 1885
北海茫月
北海茫月 2021-01-23 02:50

I am using postman extension for post request. I want to make same request with android. I used retrofit library for access my goal. But I can\'t get successful result. Where is

3条回答
  •  感情败类
    2021-01-23 03:34

    if you are using Retrofit 2.x, Try to change your build of Retrofit object as below :

    Retrofit retrofit = new Retrofit.Builder()
                            .baseUrl("http://myurl.com/")
                            .build();
    

    maybe the things below will help

    the leading / within the partial url overrides the end of API endpoint definition. Removing the / from the partial url and adding it to the base url will bring the expected result.

    Example:

    The API interface

        public interface UserService {  
        @POST("/me")
        Call me();}
    

    Build Retrofit with baseUrl

        Retrofit retrofit = Retrofit.Builder()  
        .baseUrl("https://your.api.url/v2");
        .build();
    

    Then Call :

    UserService service = retrofit.create(UserService.class);
    

    --> The request Url will be https://your.api.url/me (/v2 has been disapear)

    PRO TIP

    use relative urls for your partial endpoint urls and end your base url with the trailing slash /.

    Interface

    public interface UserService {  
        @POST("me")
        Callme();
    }
    

    Retrofit with baseURL

    Retrofit retrofit = Retrofit.Builder()  
        .baseUrl("https://your.api.url/v2/");
        .build();
    

    Call

    UserService service = retrofit.create(UserService.class);
    

    --> The request URL will be https://your.api.url/v2/me

提交回复
热议问题