I have an android client sending an http post request to my server. There are three fields in which data can be POST-ed, "from, to and message". This works fine. I have a condition where if only the "from" field is filled out, and the "to" is left blank then it has to look through the database table for all entries with that entry.The issue is at this point I want the server, after reading all those rows with that variable's name, put those rows in an array, convert that array into a JSON array and output that array (in a certain format) onto another page, before deleting the messages from the database.I believe this is not an efficient way of retreiving data (on the android's side) from the server, but I have the JSONParser code that works so I'm not changing it. Not at this point. So i just need, that when that condition is met, to take the array convert it to json array, put that array in a certain format, take the formatted thing and convert that to a string, then make another page output that exact formatted string. (Because the JSONParser Java code only retrieves information from a php page in that format.)I know I am not explaining this properly, but I wrote this question over a couple times before I asked it (so I can explain it how I understand it).The 1st Java class, let's say we call it HttpPostClass, posts data to this server page called "process".This is the first page called "process":\[code\]<?php/* connect to database */$db = mysql_connect("localhost", "root", "root");if (!$db) die('could not connect');mysql_select_db('androidp2p') or die("could not select database");$from = mysql_real_escape_string($_POST['from']); $to = mysql_real_escape_string($_POST['to']);$message = mysql_real_escape_string($_POST['message']);if ($_POST['to']){ /* user wants to send a message */ $query = "INSERT INTO messages (fromuser, touser, message) VALUES ('$from', '$to', '$message')"; mysql_query($query) or die("\n\ndatabase error!\n". mysql_error()); echo "ok. Messages have been saved.";}else { /* user wants to retrieve his messages*/ $query = "SELECT * FROM messages WHERE touser='$from'"; $result = mysql_query($query) or die("\n\ndatabase error!\n". mysql_error()); $message_count = mysql_num_rows($result); if( $message_count == 0) { /* no messages*/ echo "ok. There are no messages."; } else { /* output messages in a JSON Array*/ $get_msgs = mysql_query("SELECT * FROM messages WHERE touser='$from'"); $mailbox = array(); while($row = mysql_fetch_assoc($get_msgs)) { $mailbox[] = $row; } // this is the format I want the array to be $name = "{ \"mailbox\":".json_encode($mailbox)." }"; // now here, how do I simply forward this to next page so it can output: echo $name; /* clear messages */ $query = "DELETE FROM messages WHERE touser='$from'"; mysql_query($query) or die("\n\ndatabase error!\n". mysql_error()); echo "ok. Cleared all messages."; }}?>\[/code\]Here is my Java class that started this whole thing:\[code\]public class test extends Activity{ String number, svr_from, svr_to, svr_msg, svr_timestamp, data; // The URL private static String url = "http://192.xxx/androidp2p/test.php"; // For logging errors private static final String TAG = test.class.getSimpleName(); // JSON Node names private static final String TAG_MAILBOX = "mailbox"; private static final String TAG_FROM = "fromuser"; private static final String TAG_TO = "touser"; private static final String TAG_MSG = "message"; private static final String TAG_TIMESTAMP = "timestamp"; // contacts JSONArray JSONArray msgs_array = null; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.test); // Call the first HTTP post startService(new Intent(this, postTo.class)); new JRequest().execute(""); Log.d(TAG, "Finished test."); } public class JRequest extends AsyncTask<String, Integer, String> { @Override protected String doInBackground(String... params) { // TODO Auto-generated method stub // Creating JSON Parser instance JSONParser jParser = new JSONParser(); // getting JSON string from URL JSONObject json = jParser.getJSONFromUrl(url); try { // Getting Array of Messages msgs_array = json.getJSONArray(TAG_MAILBOX); // looping through All Messages for (int i = 0; i < msgs_array.length(); i++) { JSONObject c = msgs_array.getJSONObject(i); // Create a new message entry try{ String svr_from = c.getString(TAG_FROM); String svr_to = c.getString(TAG_TO); String svr_msg = c.getString(TAG_MSG); String svr_timestamp = c.getString(TAG_TIMESTAMP); androidDB entry = new androidDB(test.this); entry.open(); entry.createEntry(svr_from, svr_to, svr_msg, svr_timestamp); entry.close(); }catch (Exception e){ e.printStackTrace(); } } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return data; } protected void onPostExecute(String data) { } }}\[/code\]The next Java class used with the first http post (to have the condition in the server be met):\[code\]public class postTo extends Service { private static final String TAG = postTo.class.getSimpleName(); @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); Log.d(TAG, "On Create'd post2"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { // Start polling messages new postData().execute(""); Log.d(TAG, "On Start'd post2"); return super.onStartCommand(intent, flags, startId); } public class postData extends AsyncTask<String, Integer, String>{ protected void onPreExecute(String f){ // called before doInBackground has started f = "f"; } protected String doInBackground(String... params) { // TODO Auto-generated method stub String datahttp://stackoverflow.com/questions/15866915/= ""; HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost("http://192.xxx/androidp2p/process.php"); try { List<NameValuePair> pairs = new ArrayList<NameValuePair>(3); pairs.add(new BasicNameValuePair("from", "magneto")); pairs.add(new BasicNameValuePair("to", "")); pairs.add(new BasicNameValuePair("message", "")); post.setEntity(new UrlEncodedFormEntity(pairs)); HttpResponse response = client.execute(post); // Convert the response into a String HttpEntity resEntity = response.getEntity(); data = http://stackoverflow.com/questions/15866915/EntityUtils.toString(resEntity); } catch (UnsupportedEncodingException uee) { uee.printStackTrace(); } catch (ClientProtocolException cpe) { cpe.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } client.getConnectionManager().shutdown(); return data; } protected void onProgressUpdate(Integer...progress){ // called when the background task has made any progress } protected void onPostExecute(){ // called after doInBackground has finished } } @Override public synchronized void onDestroy() { super.onDestroy(); // Stop post2 messages Log.d(TAG,"On Destroy'd post2"); }}\[/code\]And here is my JSONParser code, if you need to see that:\[code\]public class JSONParser { static InputStream is = null; static JSONObject jObj = null; static String json = ""; // constructor public JSONParser() { } public JSONObject getJSONFromUrl(String url) { // Making HTTP request try { // defaultHttpClient DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); 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(); } 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); } catch (JSONException e) { Log.e("JSON Parser", "Error parsing data " + e.toString()); } // return JSON String return jObj; } }\[/code\]I'm just trying to make you clear with what is going on because I can't really explain it with jargon. Thanks for any help, and any suggestions of simple ways to improving this code (no matter how frustrating it is for you good programmers out there to see noob-ish explanation and code like this), it will be well appreciated. Keegs