【问题标题】:How to get the JSON Object in PHP from Android OKHttp如何从 Android OKHttp 获取 PHP 中的 JSON 对象
【发布时间】:2018-01-23 23:49:20
【问题描述】:

我在这里有我的 OkHttp 代码(我在 Android 中工作)

    void postRequest(String postUrl, String postBody) throws IOException {
        OkHttpClient client = new OkHttpClient();
        RequestBody body = RequestBody.create(JSON,postBody);
        Request request = new Request.Builder()
            .url(postUrl)
            .post(body)
            .build();

    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            call.cancel();
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            Log.d("TAG",response.body().string());
        }
    });
}

这是我的 PHP 部分

<?php
    header("Content-type: application/json; charset=utf-8");

    include("conexion.php");

    $nombre = $_POST["nombre"];
    $apellidoPaterno = $_POST['apellidoPaterno'];
    $apellidoMaterno = $_POST['apellidoMaterno'];
    $direccion = $_POST['direccion'];
    $redesSociales = $_POST['redesSociales'];
    $telefono = $_POST['telefono'];
    $nombreUsuario = $_POST['nombreUsuario'];
    $contrasena = $_POST['contrasenaUsuario'];

?>

我想获取通过我的 JSON 传递的值,但是当我使用 $_POST 时,它们以没有值结尾。我已经尝试使用reqres 的 API,它确实发送了信息。

感谢任何帮助,谢谢。

【问题讨论】:

  • 试试$fgc = file_get_contents("php://input");,然后是var_dump($fgc);,看看你得到了什么。
  • string(224) "{ "nombre": "A", "apellidoPaterno": "A", "apellidoMaterno": "A", "direccion": "a", "redesSociales": “A”、“telefono”:“4”、“nombreUsuario”:“a”、“contraseñaUsuario”:“A”、“confirmar”:“”}”

标签: php android json


【解决方案1】:

按照您和我的 cmets,您可以执行以下操作:

<?php
// header("Content-type: application/json; charset=utf-8"); // not really needed here for now
    include("conexion.php");
    $fgc = file_get_contents("php://input");
    $json = json_decode($fgc, true);
    // now you've got all your values in $json:
    $nombre = $json["nombre"];

或者你可以这样做:

    $json = json_decode($fgc);
    // now you've got all your values as an object in $json:
    $nombre = $json->nombre;   

进一步阅读:http://php.net/manual/de/wrappers.php.php#wrappers.php.input

【讨论】:

    【解决方案2】:

    试试这个:

    //this only you use to issue a response in json format from your php to android
    //header("Content-type: application/json; charset=utf-8");
    
    include("conexion.php");
    
    //The following lines serve to receive a json and transform them to the variables
    $data = json_decode($_POST);
    
    $nombre = $data->nombre;
    $apellidoPaterno = $data->apellidoPaterno;
    $apellidoMaterno = $data->apellidoMaterno;
    $direccion = $data->direccion;
    $redesSociales = $data->redesSociales;
    $telefono = $data->telefono;
    $nombreUsuario = $data->nombreUsuario;
    $contrasena = $data->contrasenaUsuario;
    

    当然,一切都取决于您如何武装发送的帖子主体,另一方面,如果您从 android 向您的 php 发出帖子请求,则不需要将变量转换为 json,只需传递身体和已经。

    您必须仅将您的 php 对 android 的答案转换为 JSON。

    示例:https://ideone.com/x2ENdd

    【讨论】:

    • 我不知道您在做什么或如何发送链,但对于您放置在您发送的字符串中的样本,它应该可以工作。示例:ideone.com/x2ENdd
    猜你喜欢
    • 2013-05-03
    • 2016-11-16
    • 2022-01-24
    • 2015-06-22
    • 1970-01-01
    • 2016-09-16
    • 2023-02-26
    • 1970-01-01
    • 2013-03-05
    相关资源
    最近更新 更多