Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7998491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:13:26+00:00 2026-06-04T15:13:26+00:00

im retrieving the Array of JSON data and trying to diplay and access it.But

  • 0

im retrieving the Array of JSON data and trying to diplay and access it.But when i tried to display it it only displays the last row from the array of JSON data.And when i tried to do something like json1.get(2)(json1 here is JSONArray containing JSONObject) i get array out of bound error.
Please tell me what im doing wrong and how can i retrive the JSON
Below is the relevant code to this question.

Thank You.

im calling getAllFreebies function of UserFunctions.java class from Freebies.java class

UserFunctions uf = new UserFunctions();
JSONObject json = uf.getAllFreebies();

Below is the code of my getAllFreebies() function from UserFunctions.java class:

public JSONObject getAllFreebies(){
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("tag", getAllFreebies_tag));
    JSONObject json  = jsonParser.getJSONFromUrl(getAllFreebiesURL,params);
    JSONArray json1 = new JSONArray();
    json1.put(json);
    try {
        System.out.println(json1.get(2));
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return json;
}

Below is the code from index.php class where im calling getFreebies() function of DB_Functions.php class

else if($tag = 'getAllFreebies'){
                $username = "";
                $catagory = "";
                $subcatagory = "";
                $title = "";
                $condition = "";
                $description = "";
                $address = "";
                $city = "";
                $state = "";
                $country = "";
                $zipcode = "";
                $posted_on= "";
                    $getAllFreebies = $db->getFreebies($username,$catagory,$subcatagory,$title,$condition,$description,$address,$city,$state,$country,$zipcode,$posted_on);
            $data = array();
            while($row = mysql_fetch_assoc($getAllFreebies)) {
            $response["success"] = 1;
            $data[] = array(
            $response["getAllFreebies"]["username"] = $row["username"],
            $response["getAllFreebies"]["catagory"] = $row["catagory"],
            $response["getAllFreebies"]["subcatagory"] = $row["subcatagory"],
            $response["getAllFreebies"]["title"] = $row["title"],
            $response["getAllFreebies"]["item_condition"] = $row["item_condition"],
            $response["getAllFreebies"]["description"] = $row["description"],
            $response["getAllFreebies"]["address"] = $row["address"],
            $response["getAllFreebies"]["city"] =  $row["city"],
            $response["getAllFreebies"]["state"] = $row["state"],
            $response["getAllFreebies"]["country"] = $row["country"],
            $response["getAllFreebies"]["zipcode"] = $row["zipcode"],
            $response["getAllFreebies"]["posted_on"] = $row["posted_on"]);}
            echo json_encode($response);
                    }// end of getAllFreebies tag

Below is the code of getFreebies() function of DB_Functions.php

public function getFreebies(){
$result = mysql_query("SELECT * FROM freebie") or die(mysql_error());
return($result);
}

UPDATE:I added the following in my JSONParser class in order to parse JSONArray
public JSONArray getJSONArrayFromUrl(String url, List params) {

    // Making HTTP request
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new UrlEncodedFormEntity(params));
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "n");
        }
        is.close();
        json = sb.toString();

        Log.e("JSON", json);
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
        jarray.put(jObj);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jarray;

}

And I changed getAllFreebie() to retrieve JSONArray

public JSONArray getAllFreebies() {
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("tag", getAllFreebies_tag));
    JSONArray json = jsonParser.getJSONArrayFromUrl(getAllFreebiesURL,
            params);
    return json;
}

Still im only getting single row instead of all the rows.Can anyone pls tell me what am i doing wrong.Thanks 🙁

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T15:13:27+00:00Added an answer on June 4, 2026 at 3:13 pm

    Update your code to this:

    // Initialize the array with your JSON string.
    JSONArray json1 = new JSONArray(json);
    
    // This line adds your String as the first element to
    // the JSON array and is incorrect. You can safely remove it.
    //json1.put(json);
    

    EDIT: Try this updated version of the code you posted:

    // Making HTTP request
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new UrlEncodedFormEntity(params));
        HttpResponse httpResponse = httpClient.execute(httpPost);
    
        try {
            // Get our response as a String.
            String jsonString = EntityUtils.toString(httpResponse.getEntity());
            Log.d(TAG, "JSON: "+jsonString);
    
            // Parse the JSON String into a JSONArray object.
            return new JSONArray(jsonString);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I must be missing something simple here, but I'm having trouble retrieving data from
I'm writing an API for retrieving data from a JDBC-connected Java Servlet via JSON.
I'm having difficulty in retrieving values from a Json array. I have a Json
I am having problem with $_GET array. More precisely I'm retrieving data from mysql
I am retrieving data via JSON and PHP from a URL. I am having
I need help in retrieving a JSON object and parsing the array into my
I can grab a random value from an array-like structure by retrieving a random
I am retrieving 3 fields from a database, and by default, only the username
I am retrieving data from an Oracle database and binding the same to a
I am retrieving a byte array of a pictureStream from a .net webservice. the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.