I tried to retrieve multiple rows of JSON data and display it but im not able to retrieve all the rows,im only getting the top(first) row of the database table.And im confused about how can i put the retrieved JSON data into some sort of array and access the individual rows?
I have provided the code relevant to the problem.
Yes i have done enough research before posting this question.I actually found one question which was some what similar to mine but nobody has answered it so i’m making this post.
Thank You
Below is the code from my Freebies.java class where i’m trying to retrieve JSONdata by calling getAllFreebies function from UserFunctions.java class
UserFunctions uf = new UserFunctions();
JSONObject json = uf.getAllFreebies();
System.out.println(json);
Below is the code of the function getAllFreebies() in the class UserFunctions.java
public JSONObject getAllFreebies(){
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", getAllFreebies_tag));
JSONObject json = jsonParser.getJSONFromUrl(getAllFreebiesURL,params);
return json;
}
Below is the code from index.php where im calling getFreebies() function from DB_Function.php file
else if($tag = 'getAllFreebies'){
$getAllFreebies = $db->getFreebies($username,$catagory,$subcatagory,$title,$condition,$description,$address,$city,$state,$country,$zipcode,$posted_on);
if($getAllFreebies)
{
$response["success"] = 1;
$response["getAllFreebies"]["username"] = $getAllFreebies["username"];
$response["getAllFreebies"]["catagory"] = $getAllFreebies["catagory"];
$response["getAllFreebies"]["subcatagory"] = $getAllFreebies["subcatagory"];
$response["getAllFreebies"]["title"] = $getAllFreebies["title"];
$response["getAllFreebies"]["item_condition"] = $getAllFreebies["item_condition"];
$response["getAllFreebies"]["description"] = $getAllFreebies["description"];
$response["getAllFreebies"]["address"] = $getAllFreebies["address"];
$response["getAllFreebies"]["city"] = $getAllFreebies["city"];
$response["getAllFreebies"]["state"] = $getAllFreebies["state"];
$response["getAllFreebies"]["country"] = $getAllFreebies["country"];
$response["getAllFreebies"]["zipcode"] = $getAllFreebies["zipcode"];
$response["getAllFreebies"]["posted_on"] = $getAllFreebies["posted_on"];
echo json_encode($response);
}else {
$response["error"] =1;
$response["error_msg"] = "Error in getAllFreebies";
echo json_encode($response);
}
}// end of getAllFreebies tag
Below is the code of my getFreebies() function of DB_function.php which is responsible for performing queries on MySQL database.
public function getFreebies(){
$result = mysql_query("SELECT * FROM freebie") or die(mysql_error());
return mysql_fetch_array($result);
}
Below is the logcat:
05-30 00:13:23.960: E/JSON(318): {"tag":"getAllFreebies","success":1,"error":0,"getAllFreebies":{"username":"viking","catagory":"Art","subcatagory":"Potrait","title":"Potrait","item_condition":"Good","description":"potarit","address":"Blah St","city":"lalaland","state":"NA","country":"NA","zipcode":"blah","posted_on":"2012-05-27"}}
mysql_fetch_array() only returns a single row of the query result set as an array. It does not fetch ALL of the rows. Since you’re returning the results of the fetch from your getFreebies method, instead of the result handle itself, it is impossible for the calling code to get any other results from the query, other than the one row you’ve fetched.
As such, you should have:
and