无法使用retrofit2 android

时间:2017-06-14 17:16:10

标签: php android retrofit2

我想使用改装注册和登录,但是徒劳无功,我的注册服务是

<?php

    $name= $_POST['name'];
    $email = $_POST['email'];
    $password= $_POST['password'];
    $encpass = md5($password);
    $gender= $_POST['gender'];

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

    if($query){
        $json = array("name" => $name, "email" => $email, "password" => $encpass,'gender' => $gender); 

            header('content-type: application/json');
            echo json_encode($json);
    }

    else{
        $json = array("status" => "Invalid"); 

            header('content-type: application/json');
            echo json_encode($json);
    }

    mysqli_close($con);

?>

我的登录服务是

<?php
session_start();

    $email=$_POST['email'];
    $password=$_POST['password'];
    $encpass = md5($password);
    $con = mysqli_connect("localhost", "root", "smpwd", "apidb");
    $query= mysqli_query($con,"Select * from users where email= '$email' and password='$encpass'");

    $result=mysqli_fetch_array($query);

    if($result[0]>0){
        $json = array("email" => $email, "password" => $encpass); 

            header('content-type: application/json');
            echo json_encode($json);
    }

    else{
        $json = array("status" => "Invalid"); 

            header('content-type: application/json');
            echo json_encode($json);
    }

    mysqli_close($con);
?>

我的模特是

public class User implements Serializable{

    @Expose
    public String name;
    @Expose
    public String email;
    @Expose
    public String password;
    @Expose
    public String gender;

    public User(){
        //
    }
    ....
    // omitting get/set methods
}

服务是

public interface ApiService {

    @GET("/AllJobs.php")
    Call<List<Job>> getJobData();

    @FormUrlEncoded
    @POST("/login.php")
    Call<User> loginUser(@Field("email") String email,
                          @Field("password") String password);

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

此时注册使用此代码

效果很好
mRegisterButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (validateForm()){
            mUserCall = mRestManager.getApiService()
                    .createUser(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();
                    Log.e("RESPONSE", "Is " + user1.toString());
                    Toast.makeText(getApplicationContext(), user1.name , Toast.LENGTH_SHORT).show();
                }

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

但是当我尝试使用下面的代码登录时

mLoginBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (validateForm()) {

            mUserCall = mRestManager.getApiService()
                    .loginUser(mEmailEditText.getText().toString(),
                            mPasswordEditText.getText().toString());

            Log.e("EMAIL", "email " + mEmailEditText.getText().toString());
            Log.e("Password", "password " + mPasswordEditText.getText().toString());

            mUserCall.enqueue(new Callback<User>() {
                @Override
                public void onResponse(Call<User> call, Response<User> response) {
                    User user1 = response.body();
                    Log.w("RESPONSE", "Is " + user1.toString());
                }

                @Override
                public void onFailure(Call<User> call, Throwable t) {
                    Log.w("RETRO_LOGIN_ERROR", " message is " + t.getMessage()
                            + "\n" + call.request().url());
                }
            });
        }
    }
});

我永远无法向服务器发送数据并始终收到消息

{"status":"Invalid"}

不知道为什么会发生这种情况,我们非常感谢任何帮助...

0 个答案:

没有答案