0

I have the following PHP code which is adding each three entries as a different object in the array.

  //Read the username and password from ServerRequest class
$username = $_POST["username"];   

    //Select all rows when the username match the input
$statement = mysqli_prepare($con, "SELECT * FROM Chore WHERE username = ?");
mysqli_stmt_bind_param($statement, "s", $username);
mysqli_stmt_execute($statement);

    //store the results of the previous query and create a variable for each column returned
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement,$chore_name ,$point_value, $parent_username, $child_username, $created, $points_given);
$chore = array();

    //store the previous variables you created in the array (only the ones you want to use)

    while(mysqli_stmt_fetch($statement)) {
      $t = new stdClass();
      $t->chore_name     = $chore_name;
      $t->child_username = $child_username;
      $t->point_value    = $point_value;

      $chore[] = $t;
    }

    //encode the array in JSON format
echo json_encode($chore);

//close the statement 
mysqli_stmt_close($statement);

    //Close the database connection 
mysqli_close($con);

However I am unsure as to how to create these into a listview in an activity. I have this in the xml file

 <ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/choreList"
    android:textSize="20dp"
    android:typeface="sans"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/currentChores"
    android:paddingBottom="10dp"
    android:paddingTop="50dp">
</ListView>

And the following is the code I have so far but I'm pretty sure it's completely wrong and have no idea what section of the code to put it in.

     Bundle extras = getIntent().getExtras();
    final String user = extras.getString("username");


    //The URL is the location of the PHP file to validate a user
    String requestURL = SERVER_ADDRESS+"FetchChoreData.php";

    URL url;
    Chore returnedChore = null;
    try {
        //Opens the connection to the PHP files
        //Sets the conditions of the connection
        url = new URL(requestURL);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(CONNECTION_TIMEOUT);
        conn.setConnectTimeout(CONNECTION_TIMEOUT);
        conn.setRequestMethod("POST");
        conn.setDoInput(true);
        conn.setDoOutput(true);

        //Opens an output stream to send the data to be verified
        // and then closes the all output streams and flushes the output writer
        OutputStream os = conn.getOutputStream();
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));

        Uri.Builder builder = new Uri.Builder()
                .appendQueryParameter("username",user);

        String query = builder.build().getEncodedQuery();

        writer.write(query);
        writer.flush();
        writer.close();
        os.close();
        //saves the response code to ensure connection was succesful
        int code = conn.getResponseCode();
        Log.d("code", Integer.toString(code));

        //Opens an input stream to retrieve verified data back from the server
        //Starts a String Builder to read the data off the input
        //Closes the BufferedReader once it has finished building the string
        InputStream responseStream = new BufferedInputStream(conn.getInputStream());
        BufferedReader responseStreamReader = new BufferedReader(new InputStreamReader(responseStream));
        String line;
        StringBuilder stringBuilder = new StringBuilder();
        while ((line = responseStreamReader.readLine()) != null)
            stringBuilder.append(line + "/n");
            responseStreamReader.close();

            String response = stringBuilder.toString();
            Log.d("response",response);

            JSONObject jsonResponse = new JSONObject(response);

            //Creates a JSON object from the string
            ArrayList<String> chores = new ArrayList<String>();
            for(int i=0; i < chores.length() ; i++) {
                JSONObject json_data = jsonResponse.getJSONObject(i);

                String chore=json_data.getString("chore_name");
                String username=json_data.getString("child_username");
                int points=json_data.getInt("point_value");
                String currentChore = chore + "\t" + username + "\t" + points;
                chores.add(currentChore);
                Log.d("Output", currentChore);


            ArrayAdapter<String> choreAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, chores));
            setListAdapter(choreAdapter);

            ListView listView = (ListView) findViewById(R.id.choreList);
            listView.setAdapter(choreAdapter);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } catch(JSONException e){
        e.printStackTrace();
    } catch(IOException e) {
        e.printStackTrace();
    }
2
  • possible duplicate stackoverflow.com/questions/31449887/…
    – Arshad
    Mar 12, 2016 at 18:50
  • i think the problem is here : JSONObject jsonResponse = new JSONObject(response); you need to use jsonResponse.getJSONArray("name of array"); after JSONObject jsonResponse = new JSONObject(response); Mar 12, 2016 at 20:50

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.