Retrofit Post Parameter

69,597

Solution 1

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
    );
}

Solution 2

You can also pass multiple field parameter: for example:

@FormUrlEncoded
@POST("/oauth/access_token")
void getToken(
    @FieldMap Map<String, String> params, 
    Callback<FacebookLoginUserResponse> callback
);

Solution 3

Retrofit 2.0 version:

@FormUrlEncoded
@POST("api/v2/users/sign_in")
Call<SignInResult> userSignIn(
        @FieldMap HashMap<String, String> authData
);

Solution 4

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;
}

Solution 5

"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/

Share:
69,597

Related videos on Youtube

Dipen Patel
Author by

Dipen Patel

I am CEO / Founder of AppTrait Solutions, Best mobile application development company. We develop games and apps for iOS and Android platform.

Updated on June 08, 2020

Comments

  • Dipen Patel
    Dipen Patel almost 4 years

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

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

    Below is my code

    import java.util.HashMap;
    import java.util.Map;
    
    import retrofit.Callback;
    import retrofit.http.*;
    
    
    
    
    //Myapi.java
    
    import java.util.HashMap;
    import java.util.Map;
    
    import retrofit.Callback;
    import retrofit.http.*;
    
    public interface MyApi {
    
        /* LOGIN */
        @POST("/api/0.01/oauth2/access_token/")
        // your login function in your api
        public void login(@Body HashMap<String, String> arguments, Callback<String> calback);
    }
    
    
    //In my activity
    RestAdapter restAdapter = new RestAdapter.Builder()
                    .setEndpoint(Constants_Interface.URL).setClient(newclient)
                    .build();
    
            MyApi mylogin = restAdapter.create(MyApi.class); 
    HashMap<String, String> dicMap = new HashMap<String, String>();
    dicMap.put("client_id", XXX);
            dicMap.put("client_secret", XXX);
            dicMap.put("username", XXX);
            dicMap.put("password", XXX);
    mylogin.login(dicMap, new Callback<String>() {
    
                @Override
                public void failure(RetrofitError retrofitError) {
                    retrofitError.printStackTrace(); // to see if you have
                                                        // errors
                }
    
                @Override
                public void success(String s, retrofit.client.Response response) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(), "Login Succes",
                            Toast.LENGTH_LONG).show();
    
                }
            });
    

    Below it logcat output.

    02-10 13:02:43.846: W/System.err(30684): retrofit.RetrofitError: com.squareup.okhttp.internal.http.HttpMethod.METHODS 02-10

    • Kevin Crain
      Kevin Crain about 9 years
      If you're server is expecting Json then try making the @Body argument a class.
    • Dipen Patel
      Dipen Patel about 9 years
      No it is not expecting json object. It is asking parameter as string only.
    • Kevin Crain
      Kevin Crain about 9 years
      Retrofit serializes your @Body class by default so Im sure your server will be getting a string
    • Dipen Patel
      Dipen Patel about 9 years
      I have updated the logcat output can you please look over it?
    • Kevin Crain
      Kevin Crain about 9 years
      Thats quite easy make a LoginResponse class with the fields expected from the server and use Callback<LoginResonse> rather than Callback<String>, Retrofit is trying to deserialize.
    • Dipen Patel
      Dipen Patel about 9 years
      I tried @FormUrlEncoded @POST("/api/0.01/oauth2/access_token/") // your login function in your api public void login(@Field("client_id") String client_id, @Field("client_secret") String client_secret, @Field("username") String username, @Field("password") String password, Callback<String> calback);
  • Gowtham Raj
    Gowtham Raj about 9 years
    can u paste the stack trace in pastebin ans share the link here?
  • Dipen Patel
    Dipen Patel about 9 years
    I already tried with field parameter as well. @FormUrlEncoded @POST("/api/0.01/oauth/access_token/") // your login function in your api public void login(@Field("client_id") String client_id, @Field("client_secret") String client_secret, @Field("username") String username, @Field("password") String password, Callback<String> calback);
  • IgorGanapolsky
    IgorGanapolsky about 9 years
    What is I want to use application/json MIME type? Should I still use @FormUrlEncoded
  • Gowtham Raj
    Gowtham Raj almost 9 years
    if u remove the callbacks then how will u respond to the response
  • Ehsan
    Ehsan over 8 years
    I also can not access the parameters by using $_POST['username'] in my PHP code.
  • Gowtham Raj
    Gowtham Raj over 8 years
    are u sure u used the same @FormUrlEncoded and @Field ?
  • kartheeki j
    kartheeki j over 8 years
  • Sheepdogsheep
    Sheepdogsheep over 7 years
    Thank you! This saved my life, thought I was going to have to upgrade to Retrofit 2. I was getting an error like retrofit.RetrofitError: No field METHODS, and this fixed it.
  • zihadrizkyef
    zihadrizkyef almost 7 years
    if u remove the callbacks then how will u respond to the response