【问题标题】:Retrofit Post Parameter改造后参数
【发布时间】:2015-02-10 07:21:56
【问题描述】:

我正在实现登录功能并为此使用 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

【问题讨论】:

  • 如果您的服务器期待 Json,请尝试将 @Body 参数设为一个类。
  • 不,它不期望 json 对象。它仅将参数作为字符串询问。
  • Retrofit 默认序列化你的@Body 类,所以我确定你的服务器会得到一个字符串
  • 我已经更新了 logcat 的输出,你可以看看吗?
  • 用服务器期望的字段创建一个 LoginResponse 类并使用 Callback 而不是 Callback 非常容易,Retrofit 正在尝试反序列化。

标签: java android rest retrofit retrofit2


【解决方案1】:

试试这个

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

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

【讨论】:

  • 你可以将堆栈跟踪粘贴到 pastebin 并在此处分享链接吗?
  • 我想使用什么application/json MIME类型?我还应该使用@FormUrlEncoded
  • 如果您删除回调,那么您将如何响应响应
  • 我也无法在我的 PHP 代码中使用 $_POST['username'] 访问参数。
  • 你确定你使用了相同的@FormUrlEncoded@Field 吗?
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    改造2.0版本:

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

    【讨论】:

      【解决方案4】:

      "JSON 转换

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

      查看链接了解更多信息: http://square.github.io/retrofit/

      【讨论】:

      • 我也已经尝试过使用字段参数。 @FormUrlEncoded @POST("/api/0.01/oauth/access_token/") // 你的 api 中的登录函数 public void login(@Field("client_id") String client_id, @Field("client_secret") String client_secret, @ Field("username") String 用户名,@Field("password") String 密码,Callback calback);
      【解决方案5】:

      我今天收到了这个错误

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

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

      【讨论】:

      • 谢谢!这挽救了我的生命,以为我将不得不升级到 Retrofit 2。我收到了一个类似于 retrofit.RetrofitError: No field METHODS 的错误,这解决了它。
      【解决方案6】:

      你可以像这样使用这个类:

      public interface SafeUserApi {
          @POST("/api/userlogin")
          void getUserLogin(@Body PostData postData);
      }
      
      public class PostData{
            String client_id;
            String client_secret;
            String username;
            String password;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-05-15
        • 2016-01-16
        • 2016-03-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多