【问题标题】:Failed to send parameter to PHP POST parameter android无法将参数发送到 PHP POST 参数 android
【发布时间】:2016-06-19 14:32:14
【问题描述】:

我目前正在使用android volley并尝试通过发送productID来选择产品详细信息以获取产品的详细信息。

JSONObject params = new JSONObject();
    try {
        params.put("ProductID", intent.getStringExtra("productID"));
    } catch (JSONException e) {
        e.printStackTrace();
    }

    JsonObjectRequest detailReq = new JsonObjectRequest(Request.Method.POST, url, params, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            txtNama.setText(intent.getStringExtra("nama"));
            txtManufacturer.setText(intent.getStringExtra("namaVendor"));
            txtHarga.setText("Rp. " + intent.getStringExtra("harga"));
            try {
                if(!response.getString("Foto1").equals(""))
                fotoUrl.add(response.getString("Foto1"));
                if(!response.getString("Foto2").equals(""))
                fotoUrl.add(response.getString("Foto2"));
                if(!response.getString("Foto3").equals(""))
                fotoUrl.add(response.getString("Foto3"));
                if(!response.getString("Foto4").equals(""))
                fotoUrl.add(response.getString("Foto4"));
                if(!response.getString("Foto5").equals(""))
                fotoUrl.add(response.getString("Foto5"));
                txtDesc.setText(response.getString("Deskripsi"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
            imageFragmentPagerAdapter.notifyDataSetChanged();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(ProductActivity.this, "Error occurred! Please check your internet connection!", Toast.LENGTH_LONG).show();
        }
    });
    AppController.getInstance().addToRequestQueue(detailReq);

但是 onErrorResponse 方法总是被调用。这是我的 php 代码:

<?php

require("config.inc.php");

$query = "SELECT Foto1, Foto2, Foto3, Foto4, Foto5, Deskripsi 
           FROM `product` WHERE `ProductID` = '". $_POST['ProductID']."'";

try {
    $stmt   = $db->prepare($query);
    $result = $stmt->execute();
}
catch (PDOException $ex) {
    die(json_encode($response));
}

$row = $stmt->fetch();
if ($row) {
    echo json_encode($row);
}

?> 

但是当我将 $_POST['ProductID'] 的值更改为例如 '0001' 时,php 工作得很好。任何想法?提前致谢。

【问题讨论】:

    标签: php android post params jsonobject


    【解决方案1】:
     void MakePostRequest() {
                StringRequest postRequest = new StringRequest(Request.Method.POST, EndPoints.BASE_URL_ADS,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {
                                try {
                                    JSONObject jsonResponse = new JSONObject(response);
                                    value1= jsonResponse.getString("Your ID1");
                                    value2= jsonResponse.getString("Your ID2");
    
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                    banner_id = null;
                                    full_id = null;
                                }
                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                error.printStackTrace();
                                value1= null;
                                value2= null;
                            }
                        }
                ) {
               // here is params will add to your url using post method
                    @Override
                    protected Map<String, String> getParams() {
                        Map<String, String> params = new HashMap<>();
                        params.put("app", getString(R.string.app_name));
                        //params.put("2ndParamName","valueoF2ndParam");
                        return params;
                    }
                };
                Volley.newRequestQueue(this).add(postRequest);
            }
    

    这个帖子请求使用的是这个编译com.mcxiaoke.volley:library:1.0.19 volley 版本。

    我只是添加应用名称作为参数。您可以添加更多参数。

    祝你好运

    【讨论】:

      【解决方案2】:

      您最好分别检查服务器和 Android 的问题,正如您提到的,有时服务器运行良好。

      服务器检查

      1. 您可以使用Postman,来测试服务器是否正常,或者在什么情况下,服务器是坏的。特别是检查您的服务器的响应是标准的 Json 格式。如果您想更轻松,请将$_POST 临时更改为$_GET,然后安装Chrome JSONView。然后通过您的 Chrome 进行测试。
      2. 检查数据库中的ProductID 格式。如果是text,最好先$product_id = $_POST['ProductID'],再用"$product_id"

      安卓检查

      1. 我强烈建议使用StringRequest 而不是JsonObjectRequest,手动将String 转换为JSONObject。如果响应字符串不是 JSONObject,则可以打印它并查找异常。
      2. 使用以下代码创建 JSONObject 参数: Map<String, Object> paramsMap = new HashMap<>(); paramsMap.put("ProductID", 3); JSONObject params = new JSONObject(paramsMap);

      希望能帮到你。

      【讨论】:

        【解决方案3】:

        要调试更多,您可以在 php 代码的顶部添加以下内容 var_dump($_POST);死();

        然后在 android 中查看打印的值是什么,如果什么都没有,则表示 android 没有正确发送请求。

        【讨论】:

        • 它说 org.json.JSONException: Value array(0) of type java.lang.String cannot be convert to JSONObject
        • 所以这意味着请求没有向 PHP 发出,你确定你使用了正确的 volley 格式吗?
        • 我觉得凌空系统的jsonobjectrequest有点bug,stackoverflow.com/questions/29442977/…
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多