php - 为什么我在获取 API 中的变量值时出错?
问题描述
您好,当我尝试将口音从 android 发送到 php api 时出现此错误:
注意:试图在/storage/ssd1/452/8869452/myapi.php中获取非对象的属性
我使用邮递员来测试我的 API,并用它发送了口音,一切正常,但是当我尝试从 android 发送口音时,我得到了错误。
$respuesta["mensaje"] = array();
$json=file_get_contents('php://input');
$obj=json_decode($json);
$idresp=filter_var($obj->idresp);`
我发送的 json 对象是下一个:
{
"QR":"76CD1L3hOb",
"idresp":"53",
"idportador":"4"
}
我的 android 代码是下一个
JSONObject jsonUpdate = new JSONObject();
try {
jsonUpdate.put("QR", QR);
jsonUpdate.put("idresp", idusuario);
jsonUpdate.put("idportador",txtidPortador.getText().toString().trim());
new AgregarPortador(dialogoRecuperarCuenta).execute("myURLofMyApi.php", jsonUpdate.toString());
} catch (JSONException e) {
Log.e("Error :c", "El error:\n" + e);
}//Cierra catch
asynchTask 代码:
@Override
protected String doInBackground(String... strings) {
String data = "";
HttpURLConnection httpURLConnection = null;
try {
httpURLConnection = (HttpURLConnection) new URL(strings[0]).openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
wr.writeBytes(strings[1]);
wr.flush();
wr.close();
InputStream in = httpURLConnection.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(in);
int inputStreamData = inputStreamReader.read();
while (inputStreamData != -1) {
char current = (char) inputStreamData;
inputStreamData = inputStreamReader.read();
data += current;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (httpURLConnection != null) {
httpURLConnection.disconnect();
}
}
return data;
}
解决方案
推荐阅读
- html - 在保持切换菜单(汉堡)的同时更改导航栏背景颜色
- node.js - 如何在嵌套中间件中设置 cookie 和使用上下文
- azure - 具有项目依赖项的项目的 Helm 安装失败
- reactjs - cursor.map 中的 Meteor 调用引发错误
- mongodb - 在现实中存储收集数据的最佳方式是什么?
- sql - 将 SELECT MAX(`Id`) FROM Table 传递给 setval()
- php - 使用 .htaccess 将 url 重写为文件
- node.js - 将 express 应用程序从不同模块部署到 firebase 不起作用
- c# - 如何从分数中取整数?
- scala - 如何创建键值 RDD (Scala)