0

我有以下 POST 请求,我正在使用高级 REST 客户端(Chrome 扩展)进行测试:

网址http://www.mysite.test/folder1/folder2/folder3/v1/register

参数:

  • 姓名:我的姓名
  • 姓氏:我的姓氏
  • 电子邮件:我的电子邮件
  • 密码:我的密码

结果是:

{
 error: false
 message: "You are successfully registered"
}

现在,所有这些都适用于 Advanced REST Client,但我想将其翻译为 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 个变量中。

有人可以帮帮我吗?提前致谢。

4

1 回答 1

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);}

// 在我的 MainActivity 中测试

 @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();


        }


}
于 2015-07-07T02:36:03.737 回答