首页 > 解决方案 > 为什么我在获取 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;
        }

标签: phpandroid

解决方案


推荐阅读