【发布时间】: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