改造后:JSON对象不会发送到PHP服务器 [英] Retrofit POST: JSON object does't send to PHP server

查看:49
本文介绍了改造后:JSON对象不会发送到PHP服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过Android的POST方法将User类的对象(使用gson-Retrofit)发送到服务器。如果服务器接收到任何数据,它将向App发送一个JSON对象,其中包含Key&Quot;Success&Quot;和&Quot;Message&Quot;。但遗憾的是,每次服务器向App发送{";SUCCESS&QOT;:FALSE,&QOOT;MESSAGE&QOT;:&QOOT;NO&QOOT;}时。

我认为从MainActivity调用方法时有问题。为了更好地理解,下面给出了我的所有代码:

MainActivity.java我调用此方法:

    private void checkUserValidity(User userCredential){

    ApiInterface apiInterface = RetrofitApiClient.getClient().create(ApiInterface.class);
    Call<ValidityResponse> call = apiInterface.getUserValidity(userCredential);

    call.enqueue(new Callback<ValidityResponse>() {

        @Override
        public void onResponse(Call<ValidityResponse> call, Response<ValidityResponse> response) {

            ValidityResponse validity = response.body();
            Toast.makeText(getApplicationContext(), validity.getMessage(), Toast.LENGTH_LONG).show();
        }

        @Override
        public void onFailure(Call call, Throwable t) {
            Log.e(TAG, t.toString());
        }
    });
}

我的ApiInterface.java类是:

public interface ApiInterface {
    @POST("/retrofit_login/login.php")
    Call<ValidityResponse> getUserValidity(
        @Body User userLoginCredential);
}
        

RetrofitApiClient.java类为:

public class RetrofitApiClient {

        private static final String BASE_URL = "http://192.168.0.101"; //address of your localhost
        private static Retrofit retrofit = null;

        private static Gson gson = new GsonBuilder()
                .setLenient()
                .create();

        public static Retrofit getClient() {
            if (retrofit==null) {
                retrofit = new Retrofit.Builder()
                        .baseUrl(BASE_URL)
                        .addConverterFactory(GsonConverterFactory.create(gson))
                        .build();
            }
            return retrofit;
        }
    }

User.java类为:

public class User {

    @SerializedName("user_id")
    private String userId;
    @SerializedName("password")
    private String password;

    public User(){}

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

ValidityResponse.java类为:

public class ValidityResponse {

    @SerializedName("success")
    boolean successString;
    @SerializedName("message")
    String messageString;

    public boolean isSuccess(){
        return successString;
    }

    public String getMessage() {
        return messageString;
    }
}

最后服务器端php代码为:

<?php

    if (isset($_POST['user_id']) && isset($_POST['password']))
        $json =  array('success' => true, 'message' => 'Yes');
    else
        $json =  array('success' => false, 'message' => 'No');
    
    echo json_encode($json);
?>

问题出在哪里?我现在该怎么办呢? 感谢您抽出时间。

php>

推荐答案

问题出在您的PHP代码中。试试这个:

<?php
$data = file_get_contents('php://input');
$json_data = json_decode($data , true);
if ($_SERVER['REQUEST_METHOD'] === 'POST') 
{
//code to process data
if ($data == "" || empty($json_data['user_id']) || empty($json_data['password']))
{
    $response = array('status' => false, 'message' => 'Invalid Values');    
}
else
{
    $response = array('status' => true,'message' => 'success');
}

echo json_encode($response);
}
?>

这篇关于改造后:JSON对象不会发送到PHP服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆