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 8360085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:21:16+00:00 2026-06-09T11:21:16+00:00

I am trying to learn more about MySQL and using Java (on Android) to

  • 0

I am trying to learn more about MySQL and using Java (on Android) to access and retrieve information from a database on my WAMS server. The way my app is setup is that it has an initial login screen which also grabs the “uid” of the username that’s logging in (from a different table) and stores it.

Upon login (which is functional – I setup a toast notification that displays the retrieved username and uid of the user logging in), it goes to a new screen (dashboard.xml) which has a TextView field setup to display the retrieved data (from table posted below) associated with the stored “uid”. Here is the table I am trying to pull data from:

http://db.tt/4izVQuGB

Now, I have setup a PHP file that queries my db for rows that are associated with a specific “uid”. I have tested this file using an HTML form.

$connect = mysql_connect($dbhost, $dbuser, $dbpass) or die("connection error");
mysql_select_db($dbdb)or die("database selection error");

//Retrieve the User ID
$uid = $_POST['uid'];

//Query
$query = mysql_query("SELECT * FROM node WHERE uid='$uid' AND type='goal'");

//store # of rows returned
$num_rows = mysql_num_rows($query);

if ($num_rows >= 1) {
    while($results=mysql_fetch_assoc($query)) {
        //Store the returned data into a variable
        $output = $results;

        //encode the returned data in JSON format
        echo json_encode($output);
    }
    mysql_close();
}

The result I get by testing the PHP file using uid value of 1 is:

{“nid”:”1″,”vid”:”1″,”type”:”goal”,”language”:””,”title”:”test”,”uid”:”1″,”status”:”1″,”created”:”1342894493″,”changed”:”1342894493″,”comment”:”2″,”promote”:”1″,”moderate”:”0″,”sticky”:”1″,”tnid”:”0″,”translate”:”0″}

{“nid”:”2″,”vid”:”2″,”type”:”goal”,”language”:””,”title”:”test2″,”uid”:”1″,”status”:”1″,”created”:”1342894529″,”changed”:”1342894529″,”comment”:”2″,”promote”:”1″,”moderate”:”0″,”sticky”:”1″,”tnid”:”0″,”translate”:”0″}

{“nid”:”5″,”vid”:”5″,”type”:”goal”,”language”:””,”title”:”run”,”uid”:”1″,”status”:”1″,”created”:”1343506987″,”changed”:”1343506987″,”comment”:”2″,”promote”:”1″,”moderate”:”0″,”sticky”:”1″,”tnid”:”0″,”translate”:”0″}

{“nid”:”9″,”vid”:”9″,”type”:”goal”,”language”:””,”title”:”run to the
hills”,”uid”:”1″,”status”:”1″,”created”:”1343604338″,”changed”:”1343605100″,”comment”:”2″,”promote”:”0″,”moderate”:”0″,”sticky”:”0″,”tnid”:”0″,”translate”:”0″}

Now, I have written some android code which sets up httppost and is supposed to retrieve the “titles” in my database table. I know it is wrong (obviously since it doesn’t work) but I am confused as to what to do next.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Dashboard extends Activity implements OnClickListener {

    // variable declarations
    String uid = "1";

    // create textview to display retrieved data
    TextView display;

    HttpClient httpclient;
    HttpPost httppost;
    HttpResponse httpresponse;
    HttpEntity httpentity;

    ArrayList<NameValuePair> resultArray;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dashboard);
        display = (TextView) findViewById(R.id.test);

        // initialize HttpClient
        httpclient = new DefaultHttpClient();

        // initialize HttpPost
        httppost = new HttpPost("http://192.168.1.112/android/fetch.php");

        try {
        // Create new List
        List<NameValuePair> resultList = new ArrayList<NameValuePair>();
        resultList.add(new BasicNameValuePair("uid", uid));

        httppost.setEntity(new UrlEncodedFormEntity(resultList));

        httpresponse = httpclient.execute(httppost);

        httpentity = httpresponse.getEntity();

        InputStream instream = entity.getContent();

        try {
                            // store incoming stream in an array
            JSONArray jArray = new JSONArray(streamToString(instream));
            JSONObject jData = null;

            for (int i = 0; i < jArray.length(); i++) {
                jData = jArray.getJSONObject(i);
                String goals = jData.getString("title");
                display.setText(goals);
            }
        //} catch (JSONException e) {
            //Toast.makeText(this, "No entries found", Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            Toast.makeText(this, e.toString(), Toast.LENGTH_LONG)
                    .show();
        }

    } catch (Exception e) {
        e.printStackTrace();
        Notifications error = new Notifications();
        error.userPassErrorDialog();
    }

}

private static String streamToString(InputStream is) {

    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}

public void onClick(View v) {
    // TODO Auto-generated method stub

}

}

I get the following error when testing it in the Android emulator:

http://db.tt/2vg9MqYh

Any help or suggestions will be greatly appreciated.

  • 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-09T11:21:17+00:00Added an answer on June 9, 2026 at 11:21 am

    In your Android app, you expect a JSONArray:

    // store incoming stream in an array
    JSONArray jArray = new JSONArray(streamToString(instream));
    

    However, in your PHP file you only output multiple separate JSON objects instead of a real array. I think, you should collect all items from the database in an PHP array first and then encode and output it only once.

    My PHP skills are a bit rusted, but I hope this one will work:

    //store # of rows returned
    $num_rows = mysql_num_rows($query);
    
    if ($num_rows >= 1) {
        $output = array();
    
        while($results = mysql_fetch_assoc($query)) {
            // append row to output
            $output[] = results
        }
    
        mysql_close();  // shouldn't that be outside the if block?
    
        //encode the returned data in JSON format
        echo json_encode($output);
    }
    

    I would expect the output then to be like this (maybe without indentation):

    [
        {"nid":"1","vid":"1","type":"goal","language":"","title":"test","uid":"1","status":"1","created":"1342894493","changed":"1342894493","comment":"2","promote":"1","moderate":"0","sticky":"1","tnid":"0","translate":"0"},
        {"nid":"2","vid":"2","type":"goal","language":"","title":"test2","uid":"1","status":"1","created":"1342894529","changed":"1342894529","comment":"2","promote":"1","moderate":"0","sticky":"1","tnid":"0","translate":"0"},
        {"nid":"5","vid":"5","type":"goal","language":"","title":"run","uid":"1","status":"1","created":"1343506987","changed":"1343506987","comment":"2","promote":"1","moderate":"0","sticky":"1","tnid":"0","translate":"0"},
        {"nid":"9","vid":"9","type":"goal","language":"","title":"run to the hills","uid":"1","status":"1","created":"1343604338","changed":"1343605100","comment":"2","promote":"0","moderate":"0","sticky":"0","tnid":"0","translate":"0"}
    ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to learn more about access modifiers in java, and everyone has
I've been trying to learn more about using Lamba expression trees and so I
I'm trying to learn more about database transactions, I found the ACID rule of
Right now i am trying to learn more about java threading, and i have
I'm trying to learn more about MySQL and how to protect against SQL injections
I'm trying to learn more about basic Java and the different types of Throwables,
I am trying to learn more about java. This program is an attempt to
I'm new to MySQL and am trying to learn more about the JOIN function.
I am new to Java and trying to learn more about it. I studied
I'm trying learn more about RavenDB and at the moment to focus on how

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.