0

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);

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);
}

?> 

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

zakhefron
  • 1,403
  • 1
  • 9
  • 13
user3576118
  • 375
  • 1
  • 5
  • 24

3 Answers3

2
 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);
        }

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.

Best of luck

Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41
1

You'd better check the question separately for Server and Android, as you mentioned sometimes Server works good.

Server check

  1. You can use Postman,to test whether Server is ok, or on what conditions, Server is bad. Especially check your Server's response is standard Json format. If you want do it easier, temporarily change $_POST to $_GET, and install Chrome JSONView. Then test it by your Chrome.
  2. Check ProductID format in your databases. If it is text, you'd better first $product_id = $_POST['ProductID'], and then use "$product_id"

Android check

  1. I strongly suggest use StringRequest instead of JsonObjectRequest, manually convert String to JSONObject. If response string is not JSONObject, then you can print it and find exception.
  2. use below code to create JSONObject params: Map<String, Object> paramsMap = new HashMap<>(); paramsMap.put("ProductID", 3); JSONObject params = new JSONObject(paramsMap);

Hope to help you.

yanzi1225627
  • 893
  • 11
  • 13
0

to debug more, you can add at the top of the php code the following var_dump($_POST); die();

and then see in the android what are the values printed, if you get nothing it means the android didn't send the request correctly.

Diab
  • 142
  • 7
  • it said org.json.JSONException: Value array(0) of type java.lang.String cannot be converted to JSONObject – user3576118 Jun 19 '16 at 15:11
  • so this mean that the request didn't make to the PHP, are you sure you are using the right format for volley ? – Diab Jun 19 '16 at 15:22
  • i think the volley system's jsonobjectrequest is a little buggy, http://stackoverflow.com/questions/29442977/volley-jsonobjectrequest-post-parameters-no-longer-work – user3576118 Jun 19 '16 at 15:31