【发布时间】: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”:“”}”