44

我正在实现登录功能并为此使用 Post 请求,但我收到错误消息

“改造。改造错误: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

4

6 回答 6

77

尝试使用这个

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

编辑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
    );
}
于 2015-02-10T07:24:53.077 回答
8

您还可以传递多个字段参数: 例如:

@FormUrlEncoded
@POST("/oauth/access_token")
void getToken(
    @FieldMap Map<String, String> params, 
    Callback<FacebookLoginUserResponse> callback
);
于 2015-12-23T05:47:25.227 回答
5

改造 2.0 版本:

@FormUrlEncoded
@POST("api/v2/users/sign_in")
Call<SignInResult> userSignIn(
        @FieldMap HashMap<String, String> authData
);
于 2016-01-21T03:58:17.047 回答
3

“JSON 转换

Retrofit 默认使用 Gson 将 HTTP 正文与 JSON 转换。如果您想指定与 Gson 的默认值不同的行为(例如命名策略、日期格式、自定义类型),请在构建 RestAdapter 时提供具有所需行为的新 Gson 实例。有关自定义的更多详细信息,请参阅 Gson 文档。”

有关更多信息,请参见链接:http: //square.github.io/retrofit/

于 2015-02-10T07:34:46.187 回答
3

我今天收到了这个错误

(“改造。改造错误:com.squareup.okhttp.internal.http.HttpMethod.METHODS”)

问题是我使用了不同版本的 okhttp 和 okhttp-urlconnection,所以请确保它们匹配。

于 2015-08-31T22:53:51.803 回答
3

您可以像这样使用该类:

public interface SafeUserApi {
    @POST("/api/userlogin")
    void getUserLogin(@Body PostData postData);
}

public class PostData{
      String client_id;
      String client_secret;
      String username;
      String password;
}
于 2019-06-07T10:36:32.013 回答