Web服务未发送正确的响应

时间:2017-06-15 11:59:12

标签: php android retrofit2

我必须获取具有其id的用户的数据,并且该数据将用于android usind retrofit,我的php web服务

<?php

    $con = mysqli_connect("localhost", "admin", "pwd", "api");
    $id = $_POST["id"];
    $sql= "SELECT * from users WHERE id = $id ";
    $query = mysqli_query($conn, $sql)
    $result= mysqli_fetch_array($query);

    if ($result[0]>0) {
        $json = array("name" => $result[1],"last_name" => $result[2], "email" => $result[3], "password" => $result[4],
            "city" => $result[5], "address" => $result[6], "gender" => $result[7], "dob" => $result[8], "about" => $result[9], 
            "phone_number" => $result[10]); 

            header('content-type: application/json');
            echo json_encode($json);
    } 
    else {
    $json = array("result" => "null", "status" => "invalid data"); 

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


    mysqli_close($con);
?>

我的改造服务是

@FormUrlEncoded
    @POST("/getUserDetailsById.php")
    Call<User> getUserDetail(@Field("id") int id);

在我的活动中

String uid  = mLoginPreferences.getString(Constant.USER_ID, "");
        Log.e("PREF_ID", " is " + uid);
mUserCall = mRestManager.getApiService().getUserDetail(Integer.valueOf(uid));
        mUserCall.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                User userDetails = response.body();

                if (userDetails.getEmail() != null){
                    Log.e("RESPONSE_ID", " is " + userDetails.getName());
                                        }
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {
                Log.e("DETAILS_ERROR", " message is " + t.getMessage());
            }
        });

Log.e("PREF_ID", " is " + uid);

我得到了正确的id值,但是无法从服务器返回数据总是以respnse.body()为空,想知道如何使用其id获取特定用户的数据。

2 个答案:

答案 0 :(得分:0)

您没有使用;来填写有关您收到错误的原因。

$query = mysqli_query($con, $sql)

请检查上面的行并最后放置;

答案 1 :(得分:0)

问题是您正在使用创建的其他变量,而且您还错过了行中的;

$con = mysqli_connect("localhost", "admin", "pwd", "api");

$query = mysqli_query($conn, $sql);

但您使用未创建的$conn进行查询,您应该使用的是

$query = mysqli_query($con, $sql);