i am fairly new to android development, i am trying to make a android volley request which send an ID to the php service and if it matches on the server side it should return all the value associated to it in json which is then recieved on the android app and displayed. any help will appreciated
<?php
require("include/config.php");
if (!empty($_POST)) {
$query = " SELECT * FROM music WHERE musicID = :ID " ;
$query_params = array(
':ID' => $_POST['ID'],
);
try {
$statement = $db->prepare($query);
$result = $statement->execute($query_params);
}
catch (PDOException $ex) {
$response["success"] = 0;
$response["message"] = " error";
die(json_encode($response));
}
$result = $statement->fetchAll();
if($result){
$return =array();
foreach($result as $rows){
$json = array();
$json["title"] = $rows["title"];
$json["rating"]= $rows["rating"];
$json["Price"] = $rows["Price"];
$json["Description"] = $rows["Description"];
array_push ($return,$json);
}
echo stripcslashes(json_encode($datareturn, JSON_PRETTY_PRINT));
}
} else {
}
?>
android volley request
JsonArrayRequest jsonArrayRequest=new JsonArrayRequest(URL, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
for(int i=0;i<response.length();i++){
try{
JSONObject obj=response.getJSONObject(i);
Item item=new Item();
item.setTitle(obj.getString("title"));
item.setImage(obj.getString("image"));
//add to array
array.add(item);
}catch(JSONException ex){
ex.printStackTrace();
}
}
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
AppController.getmInstance().addToRequesQueue(jsonArrayRequest,ID);
The problem is, you have a POST
request, and unfortunately, there is no direct constructor
in volley to send the POST
request for JsonArray
.
the android volley team maybe add it later but for now you can do this :
Step 1 : Create your class and extends JsonArrayRequest
then you should override.
public class CustomJsonArrayRequest extends JsonArrayRequest {
public CustomJsonArrayRequest(int method, String url, JSONObject jsonRequest, Listener<JSONArray> listener, ErrorListener errorListener) {
super(method, url, jsonRequest, listener, errorListener);
}
}
Step 2 : Now that you have a constructor to send the request you can easily send your request using it.
//If you are sending ID inside request body
JsonObject requestJson = new JsonObject();
requestJson.put("ID", "Your ID Value");
CustomJsonArrayRequest jsonArrayRequest=new CustomJsonArrayRequest(RequestMethod.POST,URL, requestJson, Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
for(int i=0;i<response.length();i++){
try{
JSONObject obj=response.getJSONObject(i);
Item item=new Item();
item.setTitle(obj.getString("title"));
item.setImage(obj.getString("image"));
//add to array
array.add(item);
}catch(JSONException ex){
ex.printStackTrace();
}
}
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
AppController.getmInstance().addToRequestQueue(jsonArrayRequest);
Also, you need to pass this ID in the url.
Let me know if you need to ask anything else. :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.