Retrofit Post Parameter

前端 未结 6 914
无人共我
无人共我 2020-12-08 06:37

I am implementing login feature and for that using Post request but i am getting error saying

\"retrofit.RetrofitError: com.squareup.okhttp.interna

相关标签:
6条回答
  • 2020-12-08 07:05

    You can use the class like this:

    public interface SafeUserApi {
        @POST("/api/userlogin")
        void getUserLogin(@Body PostData postData);
    }
    
    public class PostData{
          String client_id;
          String client_secret;
          String username;
          String password;
    }
    
    0 讨论(0)
  • 2020-12-08 07:10

    "JSON CONVERSION

    Retrofit uses Gson by default to convert HTTP bodies to and from JSON. If you want to specify behavior that is different from Gson's defaults (e.g. naming policies, date formats, custom types), provide a new Gson instance with your desired behavior when building a RestAdapter. Refer to the Gson documentation for more details on customization."

    See link for more info: http://square.github.io/retrofit/

    0 讨论(0)
  • 2020-12-08 07:16

    Retrofit 2.0 version:

    @FormUrlEncoded
    @POST("api/v2/users/sign_in")
    Call<SignInResult> userSignIn(
            @FieldMap HashMap<String, String> authData
    );
    
    0 讨论(0)
  • 2020-12-08 07:17

    You can also pass multiple field parameter: for example:

    @FormUrlEncoded
    @POST("/oauth/access_token")
    void getToken(
        @FieldMap Map<String, String> params, 
        Callback<FacebookLoginUserResponse> callback
    );
    
    0 讨论(0)
  • 2020-12-08 07:19

    Try using this

    public interface SafeUserApi {
     @FormUrlEncoded
        @POST("/api/userlogin")
        void getUserLogin(
                @Field("client_id") String id,
                @Field("client_secret") String secret,
                @Field("username") String uname,
                @Field("password") String password,
                Callback<LoginResult> cb
        );
    }
    

    Here parm1 is the POST parameter that you will be passing it to the server. This will solve your problem

    in case if you are using PHP u can access the param1 using $uname= $_POST('username');

    EDIT 1:

    retrofit 2.0 version:

    public interface SafeUserApi {
        @FormUrlEncoded
        @POST("/api/userlogin")
        Call<ResponseBody>  getUserLogin(
                @Field("client_id") String id,
                @Field("client_secret") String secret,
                @Field("username") String uname,
                @Field("password") String password
        );
    }
    
    0 讨论(0)
  • 2020-12-08 07:26

    I got this error today

    ("retrofit.RetrofitError: com.squareup.okhttp.internal.http.HttpMethod.METHODS")

    The problem was I was using different versions okhttp and okhttp-urlconnection, so make sure they match.

    0 讨论(0)
提交回复
热议问题