Java 改造后参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28426154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrofit Post Parameter
提问by Dipen Patel
I am implementing login feature and for that using Post request but i am getting error saying
我正在实现登录功能并为此使用 Post 请求,但我收到错误消息
"retrofit.RetrofitError: com.squareup.okhttp.internal.http.HttpMethod.METHODS"
“改造.改造错误: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.
在它下面 logcat 输出。
02-10 13:02:43.846: W/System.err(30684): retrofit.RetrofitError: com.squareup.okhttp.internal.http.HttpMethod.METHODS 02-10
02-10 13:02:43.846:W/System.err(30684):retrofit.RetrofitError:com.squareup.okhttp.internal.http.HttpMethod.METHODS 02-10
采纳答案by Gowtham Raj
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
这里 parm1 是您将其传递给服务器的 POST 参数。这将解决您的问题
in case if you are using PHP u can access the param1 using $uname= $_POST('username');
如果您使用的是 PHP,您可以使用以下命令访问 param1 $uname= $_POST('username');
EDIT 1:
编辑 1:
retrofit 2.0 version:
改造 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
);
}
回答by Kevin Crain
"JSON CONVERSION
"JSON 转换
Retrofit uses Gson by default to convert HTTP bodies to and from JSON.If you want to specify behavior that is different from Gson's defaults (e.g. naming policies, date formats, custom types), provide a new Gson instance with your desired behavior when building a RestAdapter. Refer to the Gson documentation for more details on customization."
Retrofit 默认使用 Gson 将 HTTP 主体与 JSON 相互转换。如果您想指定与 Gson 的默认值不同的行为(例如命名策略、日期格式、自定义类型),请在构建 RestAdapter 时提供具有所需行为的新 Gson 实例。有关自定义的更多详细信息,请参阅 Gson 文档。”
See link for more info: http://square.github.io/retrofit/
有关更多信息,请参阅链接:http: //square.github.io/retrofit/
回答by Singed
I got this error today
我今天收到这个错误
("retrofit.RetrofitError: com.squareup.okhttp.internal.http.HttpMethod.METHODS")
(“改造.改造错误:com.squareup.okhttp.internal.http.HttpMethod.METHODS”)
The problem was I was using different versions okhttp and okhttp-urlconnection, so make sure they match.
问题是我使用了不同版本的 okhttp 和 okhttp-urlconnection,因此请确保它们匹配。
回答by Ramkailash
You can also pass multiple field parameter:for example:
您还可以传递多个字段参数:例如:
@FormUrlEncoded
@POST("/oauth/access_token")
void getToken(
@FieldMap Map<String, String> params,
Callback<FacebookLoginUserResponse> callback
);
回答by Robert
Retrofit 2.0 version:
改造2.0版本:
@FormUrlEncoded
@POST("api/v2/users/sign_in")
Call<SignInResult> userSignIn(
@FieldMap HashMap<String, String> authData
);
回答by Ahmad Aghazadeh
You can use the class like this:
你可以像这样使用这个类:
public interface SafeUserApi {
@POST("/api/userlogin")
void getUserLogin(@Body PostData postData);
}
public class PostData{
String client_id;
String client_secret;
String username;
String password;
}