mercredi 22 juin 2016

Failed to send parameter to PHP POST parameter android

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.

Aucun commentaire:

Enregistrer un commentaire