Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 270
Dipen Patel Avatar asked Feb 10 '15 07:02

Dipen Patel


People also ask

What is @FormUrlEncoded in retrofit?

Annotation Type FormUrlEncodedDenotes that the request body will use form URL encoding. Fields should be declared as parameters and annotated with @Field . Requests made with this annotation will have application/x-www-form-urlencoded MIME type.

How do you pass body in GET request retrofit on Android?

This means your @GET or @DELETE should not have @Body parameter. You can use query type url or path type url or Query Map to fulfill your need. Else you can use other method annotation.


1 Answers

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     ); } 
like image 115
Gowtham Raj Avatar answered Sep 28 '22 16:09

Gowtham Raj