Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Wednesday, May 4, 2016

Retrofit Post Parameter

Retrofit Post Parameter


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 arguments, Callback calback);  }      //In my activity  RestAdapter restAdapter = new RestAdapter.Builder()                  .setEndpoint(Constants_Interface.URL).setClient(newclient)                  .build();            MyApi mylogin = restAdapter.create(MyApi.class);   HashMap dicMap = new HashMap();  dicMap.put("client_id", XXX);          dicMap.put("client_secret", XXX);          dicMap.put("username", XXX);          dicMap.put("password", XXX);  mylogin.login(dicMap, new Callback() {                @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

Answer by Gowtham Raj for Retrofit Post Parameter


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 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:

@FormUrlEncoded  @POST("/api/userlogin")  Call getuserlogin(@Field("client_id")string  id,@Field("client_secret")string  secret,@Field("username")string  uname,@Field("password")string  password, callback cb);  

Answer by Kevin Crain for Retrofit Post Parameter


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

Answer by Singed for Retrofit Post Parameter


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.

Answer by Ramkailash for Retrofit Post Parameter


You can also pass multiple field parameter: for example:

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

Answer by Robert for Retrofit Post Parameter


Retrofit 2.0 version:

@FormUrlEncoded  @POST("api/v2/users/sign_in")  Call userSignIn(          @FieldMap HashMap authData  );  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.