• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Displaying data from MySQL database

I am using a PHP script to convert my MySQL database data into JSON data. I am successful in doing so. Now I want to retrieve the data from my PHP script (on my localhost server) and parse that data and display it in a TextView. Everything runs fine, no errors, but the data is not displaying as I think it should. Here is my code for my MainActivity...

[HIGH]package com.example.testexternaldatabase;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

TextView resultView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.enableDefaults();
resultView = (TextView) findViewById(R.id.result);

getData();

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}

public void getData() {
String result = "";
InputStream isr = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpost = new HttpPost("http://10.0.0.9/myfile.php");
HttpResponse response = httpclient.execute(httpost);
HttpEntity entity = response.getEntity();
isr = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
resultView.setText("Couldn't connect to database");
}

try {
BufferedReader reader = new BufferedReader(new InputStreamReader(isr, "iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (Exception e) {
Log.e("log_tag", "Error conerting result " + e.toString());

}

try {
String s = "";
JSONArray jArray = new JSONArray(result);

for (int i = 0; i < jArray.length(); i++) {
JSONObject json = jArray.getJSONObject(i);
s = s +
"ID : " + json.getString("ID") + "\n" +
"Name" + json.getString("Name") + "\n" +
"Email" + json.getString("Email") + "\n\n";
}

resultView.setText(s);

} catch (Exception e) {
Log.e("log_tag", "Error Parsing Data" + e.toString());
}

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}

}
[/HIGH]

I have a TextView called "result" and I gave permissions to access the internet in my manifest. Any ideas? Thanks guys.
 
First order debugging - run FirePHP (in Firefox - you'll also have to install Firebug) on the server to make sure that the JSON data you're sending is the data you think you're sending. Just echo the data as-is.

Second 0order debugging - get the data in the phone. Display the JSON string as-is. Make sure the sting has the data you sent.

If you don't know that those two things are correct you may be fixing a problem that doesn't exist. (IOW, your code may be perfect, but it's working on corrupt [for what it expects] data.) Everything can be running fine on the server and the phone, but the data may not be what your code is expecting to find. IOW, me..@my@com will parse perfectly (it's a perfectly good string, which is what's expected there), but when you try to use it as an email address, the code may blow up in your face.

"not displaying as I think it should" doesn't tell us anything, so I'm not making any guesses as to what it means. Not lined up? Not spaced correctly? Name showing where the ID should be? I have no idea.
 
Back
Top Bottom