改造 Post 参数
Retrofit Post Parameter
我正在实现登录功能并为此使用 Post 请求,但我收到错误提示
"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
编辑 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
);
}
"JSON 转化
Retrofit 默认使用 Gson 将 HTTP 主体与 JSON 相互转换。 如果您想指定与 Gson 默认设置不同的行为(例如命名策略,日期格式、自定义类型),在构建 RestAdapter 时提供具有所需行为的新 Gson 实例。有关自定义的更多详细信息,请参阅 Gson 文档。”
有关详细信息,请参阅 link:
http://square.github.io/retrofit/
我今天遇到这个错误
("retrofit.RetrofitError: com.squareup.okhttp.internal.http.HttpMethod.METHODS")
问题是我使用了不同版本的 okhttp 和 okhttp-urlconnection,因此请确保它们匹配。
也可以传多个字段参数:
例如:
@FormUrlEncoded
@POST("/oauth/access_token")
void getToken(
@FieldMap Map<String, String> params,
Callback<FacebookLoginUserResponse> callback
);
Retrofit 2.0 版本:
@FormUrlEncoded
@POST("api/v2/users/sign_in")
Call<SignInResult> userSignIn(
@FieldMap HashMap<String, String> authData
);
您可以像这样使用 class:
public interface SafeUserApi {
@POST("/api/userlogin")
void getUserLogin(@Body PostData postData);
}
public class PostData{
String client_id;
String client_secret;
String username;
String password;
}
我正在实现登录功能并为此使用 Post 请求,但我收到错误提示
"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');
编辑 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
);
}
"JSON 转化
Retrofit 默认使用 Gson 将 HTTP 主体与 JSON 相互转换。 如果您想指定与 Gson 默认设置不同的行为(例如命名策略,日期格式、自定义类型),在构建 RestAdapter 时提供具有所需行为的新 Gson 实例。有关自定义的更多详细信息,请参阅 Gson 文档。”
有关详细信息,请参阅 link: http://square.github.io/retrofit/
我今天遇到这个错误
("retrofit.RetrofitError: com.squareup.okhttp.internal.http.HttpMethod.METHODS")
问题是我使用了不同版本的 okhttp 和 okhttp-urlconnection,因此请确保它们匹配。
也可以传多个字段参数: 例如:
@FormUrlEncoded
@POST("/oauth/access_token")
void getToken(
@FieldMap Map<String, String> params,
Callback<FacebookLoginUserResponse> callback
);
Retrofit 2.0 版本:
@FormUrlEncoded
@POST("api/v2/users/sign_in")
Call<SignInResult> userSignIn(
@FieldMap HashMap<String, String> authData
);
您可以像这样使用 class:
public interface SafeUserApi {
@POST("/api/userlogin")
void getUserLogin(@Body PostData postData);
}
public class PostData{
String client_id;
String client_secret;
String username;
String password;
}