问题
So I am pretty new on that library (Retrofit) and actually I am stucked on an atypical case.
I have an API where I try to register (sign up) a new user to it, to test it I use "DHC Rest Client" on Chrome. It works when I send this (see below):
Header:
Content-Type: application/x-www-form-urlencoded
Body (text)
user={"key1": "value1", "key2": "value2", etc.}
So my plan is to make the same thing on Android ! I have created an interface (see below):
Interface
interface RestRegister{
@Headers("Content-Type: application/x-www-form-urlencoded")
@POST("/api/register")
void postRegister(@Body String user, Callback<LoginResponse> callback);
}
But I always get a "Failed!" from the Callback error handler. I correctly get the correct parameters (I give it as a simple String with the correct format - "user={\"email\":\"lol@lol.us\", etc.}").
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();
}
});
}
});
Later I would like to create a JSONObject in the right format too ;).
Waiting for your answers :) !
回答1:
This is how the documentation suggests:
@FormUrlEncoded
@POST("/user/edit")
User updateUser(@Field("first_name") String first, @Field("last_name") String last);
In your case it would be:
interface RestRegister{
@FormUrlEncoded
@POST("/api/register")
void postRegister(@Field("user") String data, Callback<LoginResponse> callback);
}
And then:
restRegister.postRegister("{\"email\":\"lol@lol.us\",\"first_name\":\"jean\",\"last_name\":\"loop\",\"password\":\"test95test\",\"gender\":\"1\",\"birthdate\":\"1978/05/16\"}", ...);
回答2:
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("http://ENDPOINT:3000/")
.build();
Should be
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("http://ENDPOINT:3000")
.build();
For Example:
@FormUrlEncoded
@POST("/alert.php")
void gettingfirsttimealert(@Field("key") String value, @Field("user_id") String user_id, Callback<JsonResponseis> cb);
JSON Response
{ "success": true,
"details": {
"firstname": mathan,
"arraylist": [
"subscribe": "subscribe"
]
}
}
POJO class should be
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 Should be
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) {
}
};
来源:https://stackoverflow.com/questions/31855793/retrofit-post-raw-text-user-key1-value1-etc