我正在尝试使用改造版Android

时间:2019-08-24 07:26:04

标签: android retrofit

在尝试插入数据库时​​。空值插入的不是实际数据,请帮助我哪里出错了。

//请求 { “名称”:“ xxxx”, “ email”:“ idfjd”, “ password”:“ fb”, “ gender”:“ male”}

//响应 {     “ status”:“成功”,     “ msg”:“已成功插入” }

private void userSignUp(){

    //defining a progress dialog to show while signing up
    final ProgressDialog progressDialog = new ProgressDialog(this);
    progressDialog.setMessage("Signing Up...");
    progressDialog.show();

    //getting the user values
    final RadioButton radioSex = (RadioButton) findViewById(radioGender.getCheckedRadioButtonId());

    String name = editTextName.getText().toString().trim();
    String email = editTextEmail.getText().toString().trim();
    String password = editTextPassword.getText().toString().trim();
    String gender = radioSex.getText().toString();


    //building retrofit object
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(APIUrl.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    //Defining retrofit api service
    APIService service = retrofit.create(APIService.class);

    //Defining the user object as we need to pass it with the call
    User user = new User("asd", "ig", "hdk", "kd");

    //defining the call
    Call<Result> call = service.createUser(
            user.getName(),
            user.getEmail(),
            user.getPassword(),
            user.getGender()
    );

    //calling the api
    call.enqueue(new Callback<Result>() {
        @Override
        public void onResponse(Call<Result> call, Response<Result> response) {
            //hiding progress dialog
            progressDialog.dismiss();

            //displaying the message from the response as toast
            Toast.makeText(getApplicationContext(), response.body().getMessage(), Toast.LENGTH_LONG).show();

            //if there is no error
            if (!response.body().getError()) {
                //starting profile activity
                finish();
                SharedPrefManager.getInstance(getApplicationContext()).userLogin(response.body().getUser());
                startActivity(new Intent(getApplicationContext(), HomeActivity.class));
            }
        }

        @Override
        public void onFailure(Call<Result> call, Throwable t) {
            progressDialog.dismiss();
            Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_LONG).show();
        }
    });
}

//用户类别 公共类用户{

private int id;
private String name;
private String email;
private String password;
private String gender;

public User(String name, String email, String password, String gender) {
    this.name = name;
    this.email = email;
    this.password = password;
    this.gender = gender;
}

public User(int id, String name, String email, String gender){
    this.id = id;
    this.name = name;
    this.email = email;
    this.gender = gender;
}

public User(int id, String name, String email, String password, String gender) {
    this.id = id;
    this.name = name;
    this.email = email;
    this.password = password;
    this.gender = gender;
}

public int getId() {
    return id;
}

public String getName() {
    return name;
}

public String getEmail() {
    return email;
}

public String getPassword(){
    return password;
}

public String getGender() {
    return gender;
}

}

enter image description here

请告诉我代码有什么问题

// Apiservices类

公共接口APIService {

@FormUrlEncoded
@POST("Owner/api.php?f=test_c")
Call<Result> createUser(
        @Field("name") String name,
        @Field("email") String email,
        @Field("password") String password,
        @Field("gender") String gender);

}

// Api网址

公共类APIUrl {     公共静态最终字符串BASE_URL =“ https://dev.anyemi.com/webservices/omrooms/”; }

2 个答案:

答案 0 :(得分:0)

在用户类别中,您不需要ID,这是自动增量列,

结果类用于响应,因此它与create user无关,请在您的api接口中检查createUser方法

首先与邮递员检查您的api,以确保其正常工作

答案 1 :(得分:0)

我认为问题出在硬编码参数“?f = test_c”,翻新自动添加“?”后面跟着您通过@Field传递的参数。 尝试@POST(“ Owner / api.php”)并通过@Field传递参数。