You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React-Native Fetch Post请求在Android端失败问题排查

排查React Native Fetch Post请求在Android端失败的问题

咱们直接从你贴的PHP代码里找问题——这大概率就是Android端请求失败的根源,再给你补上前端需要注意的点:

1. JSON解析方式不匹配(致命错误)

你用了json_decode(file_get_contents('php://input'), true),这里的true参数会把JSON转成关联数组,但后面你却用对象的方式$data->blogId去访问值,这会直接抛出PHP错误(比如“Trying to get property 'blogId' of non-object”),后端返回500错误,Android的Fetch自然就失败了。

修复方案二选一:

  • 用数组语法访问:$blogId = $data['blogId'];
  • 去掉json_decode的true参数,让它返回对象:$data = json_decode(file_get_contents('php://input'));

2. SQL注入风险+潜在语法错误

你直接把$data->blogId拼进SQL语句里,这不仅有严重的SQL注入风险,万一blogId是带特殊字符的字符串(哪怕是数字,这种写法也不规范),还会导致SQL语法错误,请求自然失败。

必须修复:用PDO参数绑定,安全又可靠:

// 消息查询
$statement = $pdo->prepare("SELECT * FROM M_Messages where M_B_Id = :blogId");
$statement->bindParam(':blogId', $blogId, PDO::PARAM_INT); // 如果blogId是整数,用PARAM_INT更严谨
$statement->execute();

// 博客查询
$statement = $pdo->prepare("SELECT * FROM B_Blogs where B_Id = :blogId");
$statement->bindParam(':blogId', $blogId, PDO::PARAM_INT);
$statement->execute();

3. 缺失错误处理和响应头

你的代码完全没处理这些异常情况:

  • 前端传的JSON格式无效(json_decode返回null)
  • 请求里没带blogId字段
  • 数据库查询失败(比如连接问题、表/字段名写错)

另外,你没设置Content-Type: application/json响应头,Android端可能无法正确解析返回的JSON。

加上错误处理和响应头的完整代码:

<?php
header('Content-Type: application/json'); // 先设置JSON响应头
include('connect.php');

// 处理无效JSON或缺失参数
$data = json_decode(file_get_contents('php://input'), true);
if ($data === null || !isset($data['blogId'])) {
    echo json_encode(["error" => "请求数据无效或缺少blogId"]);
    exit;
}

$blogId = $data['blogId'];

// 捕获PDO异常
try {
    // 查询消息
    $statement = $pdo->prepare("SELECT * FROM M_Messages where M_B_Id = :blogId");
    $statement->bindParam(':blogId', $blogId, PDO::PARAM_INT);
    $statement->execute();
    $Messages = $statement->fetchAll(PDO::FETCH_ASSOC);

    // 查询博客
    $statement = $pdo->prepare("SELECT * FROM B_Blogs where B_Id = :blogId");
    $statement->bindParam(':blogId', $blogId, PDO::PARAM_INT);
    $statement->execute();
    $Blog = $statement->fetchAll(PDO::FETCH_ASSOC);

    echo json_encode(["Messages" => $Messages, "Blog" => $Blog]);
} catch (PDOException $e) {
    // 生产环境不要暴露具体错误信息,这里只是方便调试
    echo json_encode(["error" => "数据库错误: " . $e->getMessage()]);
}
?>

4. React Native端的请求检查

确保你的Fetch请求格式正确,Android端对请求格式的要求比iOS更严格:

  • 必须设置Content-Type: application/json请求头
  • 请求体要转成JSON字符串(不能直接传对象)
  • 要处理HTTP错误状态码(比如500、400)

示例代码:

fetch('你的PHP接口地址', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ blogId: 你的博客ID }),
})
.then(response => {
  if (!response.ok) {
    throw new Error(`请求失败,状态码: ${response.status}`);
  }
  return response.json();
})
.then(data => console.log('请求成功:', data))
.catch(error => console.error('请求出错:', error));

进一步调试建议

  • 查看Android的Logcat日志,里面会有Fetch的具体错误信息(比如网络错误、响应解析失败)
  • 用Postman或Curl直接调用PHP接口,看看返回的是正常JSON还是错误信息
  • 开启PHP的错误日志,查看后端具体抛出了什么错误(找php.ini里的error_log配置)

内容的提问来源于stack exchange,提问作者IFDev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:14:51