我正在实现loginfunction,并使用后请求,但我得到错误说
“retrofit.RetrofitError:com.squareup.okhttp.internal.http.HttpMethod.METHODS”
以下是我的代码
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(); } });
在它下面logcat输出。
02-10 13:02:43.846:W / System.err(30684):retrofit.RetrofitError:com.squareup.okhttp.internal.http.HttpMethod.METHODS 02-10
尝试使用这个
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 ); }
这里parm1是你将要传递给服务器的POST参数。 这将解决您的问题
如果你使用PHP,你可以使用$uname= $_POST('username');
来访问param1 $uname= $_POST('username');
编辑1:
翻新2.0版本:
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 ); }
您也可以传递多个字段参数:例如:
@FormUrlEncoded @POST("/oauth/access_token") void getToken( @FieldMap Map<String, String> params, Callback<FacebookLoginUserResponse> callback );
我今天得到这个错误
(“retrofit.RetrofitError:com.squareup.okhttp.internal.http.HttpMethod.METHODS”)
问题是我使用不同的版本okhttp和okhttp-urlconnection,所以请确保它们匹配。
Retrofit 2.0版本:
@FormUrlEncoded @POST("api/v2/users/sign_in") Call<SignInResult> userSignIn( @FieldMap HashMap<String, String> authData );
“JSON转换
Retrofit默认使用Gson来将HTTP主体转换为JSON。 如果要指定与Gson的默认值不同的行为(例如命名策略,date格式,自定义types),请在构buildRestAdapter时提供具有所需行为的新Gson实例。 有关定制的更多详细信息,请参阅Gson文档。“
查看链接了解更多信息: http : //square.github.io/retrofit/