本文介绍了无法将参数发送到PHP POST参数android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在使用android volley,并尝试通过发送productID来获取产品的详细数据来选择产品的详细信息.

I am currently using android volley and trying to select the product detail by sending the productID to get the detail data of the product.

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代码:

But the onErrorResponse method always gets called. This is my php code:

<?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可以正常工作.任何的想法?预先感谢.

But when i change the value of $_POST['ProductID'] into for example '0001', the php works just fine. Any idea? Thanks in advance.

推荐答案

 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齐射版本.

This post request is using this compile com.mcxiaoke.volley:library:1.0.19 volley version.

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

i am just adding app name as parameter.you can add more params.

祝你好运

这篇关于无法将参数发送到PHP POST参数android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 13:41