我有以下POST请求,我正在使用高级REST客户端(Chrome扩展程序)进行测试:
网址: http://www.mysite.test/folder1/folder2/folder3/v1/register
参数:
结果是:
{
error: false
message: "You are successfully registered"
}
现在,所有这些都适用于高级REST客户端,但我想将其翻译为Retrofit。
我创建了 RestClientUsers :
public class RestClientUsers {
private static final String BASE_URL = "http://www.mysite.test/folder1/folder2/folder3";
private ApiServiceUsers apiServiceUsers;
public RestClientUsers() {
Gson gson = new GsonBuilder()
.setDateFormat("dd'-'MM'-'yyyy'T'HH':'mm':'ss'.'SSS'Z'")
.create();
RestAdapter restAdapter = new RestAdapter.Builder()
.setLogLevel(RestAdapter.LogLevel.FULL)
.setEndpoint(BASE_URL)
.setConverter(new GsonConverter(gson))
.build();
apiServiceUsers = restAdapter.create(ApiServiceUsers.class);
}
public ApiServiceUsers getApiServiceUsers() {
return apiServiceUsers;
}
}
但我不知道如何处理才能获得一个完美的系统。
我想执行请求,并将错误和消息存储到2个变量中。
有人可以帮帮我吗?提前谢谢。
答案 0 :(得分:1)
你必须像这样创建一些类。
public class ObjectPost {
String myName;
String mySurname;
String myEmail;
String myPassword;
public void setMyEmail(String myEmail) {
this.myEmail = myEmail;
}
public void setMyName(String myName) {
this.myName = myName;
}
public void setMyPassword(String myPassword) {
this.myPassword = myPassword;
}
public void setMySurname(String mySurname) {
this.mySurname = mySurname;
}}
///// pojo接收
public class ObjectRecieve {
Boolean error;
String message;
public Boolean isError() {
return error;
}
public String getMessage() {
return message;
}}
//
public interface RestFul {
@POST("/v1/register")
void getValue(@Body ObjectPost mObject,Callback<ObjectRecieve> object);}
//在我的主要活动中测试
@Override
protected void onCreate(Bundle savedInstanceState) {
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("http://www.mysite.test/folder1/folder2/folder3")
.build();
RestFul restFul = restAdapter.create(RestFul.class);
ObjectPost objectPost = new ObjectPost();
objectPost.setMyName("xxxx");
objectPost.setMySurname("xxxx");
objectPost.setMyPassword("xxxx");
objectPost.setMyEmail("xxxx");
restFul.getValue(objectPost,new Callback<ObjectRecieve>() {
@Override
public void success(ObjectRecieve result, Response response) {
// receive your object here.
Log.d("TAG",""+result.getMessage())
})
@Override
public void failure(RetrofitError error) {
System.out.println("error = [" + error + "]");
Toast.makeText(getApplication(),
"Please check your internet connection.", Toast.LENGTH_SHORT)
.show();
}
}