2017-06-14 149 views
0

我創建/使用retrofit2我的服務器端註冊新用戶

<?php 

    $name= $_POST['name']; 
    $email = $_POST['email']; 
    $password= $_POST['password']; 
    $gender= $_POST['gender']; 

    $con = mysqli_connect("localhost", "root", "qwerty", "db"); 
    $query= mysqli_query($con, "INSERT INTO users(name,email, password, gender) VALUES('$name','$email', '$password', '$gender')"); 

    if($query){ 
     echo "You are sucessfully Registered"; 
    } 

    else{ 
     echo "your details could not be registered"; 
    } 

    mysqli_close($con); 

?> 

我的模型類是

public class User { 

    @SerializedName("name") 
    @Expose 
    public String name; 
    @SerializedName("email") 
    @Expose 
    public String email; 
    @SerializedName("password") 
    @Expose 
    public String password; 
    @SerializedName("gender") 
    @Expose 
    public String gender; 

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

郵政服務

@POST("register.php") 
Call<User> createUser(@Body User user); 

發佈數據

mRegisterButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       if (validateForm()){ 
        mUserCall = mRestManager.getJobService() 
          .createUser(new User(mNameEditText.getText().toString(), 
               mEmailEditText.getText().toString(), 
               mPasswordEditText.getText().toString(), 
               mGenders[position])); 
        mUserCall.enqueue(new Callback<User>() { 
         @Override 
         public void onResponse(Call<User> call, Response<User> response) { 
          User user1 = response.body(); 
          Toast.makeText(getApplicationContext(), user1.name , Toast.LENGTH_SHORT).show(); 
         } 

         @Override 
         public void onFailure(Call<User> call, Throwable t) { 

          Log.e("REGISTER_ERROR", "Message is " + t.getMessage()); 
         } 
        }); 
       } 
      } 
     }); 

我收到提示

java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ 

我已經找到了一些解決方案,並作出

@FormUrlEncoded 
    @POST("register.php") 
    Call<User> createUser(@Field("name") String name, 
          @Field("email") String email, 
          @Field("password") String password, 
          @Field("gender") String gender); 

發生相應的變化,但它也不起作用,同樣的錯誤信息,如何解決這個錯誤。

+0

使用調用

+0

添加你的json響應 –

+0

你沒有返回一個User對象,你正在爲PHP返回一個成功/失敗的字符串。 –

回答

1

檢查你的迴應,它是不同於你的期望。

Json必須從「{」也有響應是不同於你的Pojo 用戶所以從新的響應使新的Pojo。

相關問題