所以我在那个库(Retrofit)上很新,实际上我被困在一个非典型的案例中。
我有一个API,我尝试注册(注册)一个新用户,测试它我使用" DHC Rest Client"在Chrome上。发送时它会起作用(见下文):
标题:
内容类型:application / x-www-form-urlencoded
正文(文字)
user = {" key1":" value1"," key2":" value2"等。}
所以我的计划是在Android上做同样的事情!我创建了一个界面(见下文):
接口
interface RestRegister{
@Headers("Content-Type: application/x-www-form-urlencoded")
@POST("/api/register")
void postRegister(@Body String user, Callback<LoginResponse> callback);
}
但我总是得到一个&#34;失败!&#34;来自Callback错误处理程序。我正确地得到了正确的参数(我把它作为一个简单的字符串,格式正确 - &#34; user = {\&#34; email \&#34;:\&#34; lol@lol.us \& #34;等等}&#34;)。
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("http://ENDPOINT:3000/")
.build();
RestRegister restRegister = restAdapter.create(RestRegister.class);
restRegister.postRegister("user={\"email\":\"lol@lol.us\",\"first_name\":\"jean\",\"last_name\":\"loop\",\"password\":\"test95test\",\"gender\":\"1\",\"birthdate\":\"1978/05/16\"}", new Callback<LoginResponse>() {
@Override
public void success(LoginResponse loginResponse, Response response) {
Toast.makeText(SignUp.this, query, Toast.LENGTH_LONG).show();
Toast.makeText(SignUp.this, "Created !", Toast.LENGTH_LONG).show();
Intent intent = new Intent("com.signupconfirmation");
startActivity(intent);
}
@Override
public void failure(RetrofitError error) {
Toast.makeText(SignUp.this, query, Toast.LENGTH_LONG).show();
Toast.makeText(SignUp.this, "Failed !", Toast.LENGTH_LONG).show();
}
});
}
});
稍后我想以正确的格式创建一个JSONObject;)。
等待你的答案:)!
答案 0 :(得分:1)
这是文档建议的方式:
@FormUrlEncoded
@POST("/user/edit")
User updateUser(@Field("first_name") String first, @Field("last_name") String last);
在你的情况下,它将是:
interface RestRegister{
@FormUrlEncoded
@POST("/api/register")
void postRegister(@Field("user") String data, Callback<LoginResponse> callback);
}
然后:
restRegister.postRegister("{\"email\":\"lol@lol.us\",\"first_name\":\"jean\",\"last_name\":\"loop\",\"password\":\"test95test\",\"gender\":\"1\",\"birthdate\":\"1978/05/16\"}", ...);
答案 1 :(得分:-1)
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("http://ENDPOINT:3000/")
.build();
应
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("http://ENDPOINT:3000")
.build();
例如:
@FormUrlEncoded
@POST("/alert.php")
void gettingfirsttimealert(@Field("key") String value, @Field("user_id") String user_id, Callback<JsonResponseis> cb);
JSON响应
{ "success": true,
"details": {
"firstname": mathan,
"arraylist": [
"subscribe": "subscribe"
]
}
}
POJO课应该是
public class JsonResponseis{
// Json response key is String
public String success;
//Json response key is JSON OBJECT
public Details details;
public class Details {
public String firstname;
//json response key is arraylist
public List<ArrayListis> arraylist;
}
public class ArrayListis{
public String subscribe;
}
}
CallBack应该是
public Callback downloadalertcalback = new Callback() {
String alertmediapath = "null";
String ts;
@Override
public void success(Object o, Response response) {
JsonResposeis u = (JsonResposeis ) o;
if (u.success != null) {
if (u.success.equalsIgnoreCase("true")) {
//Do here
}
}
}
@Override
public void failure(RetrofitError error) {
}
};