Referencing two or more JSON tables in Android with PHP -


I currently have a PHP script that returns JSON encoded COUNT (*) from two tables:

< Pre> JSON response to // array $ response = array (); // db connect square require_once 'db_connect.php'; // db $ db = connecting to new DB_CONNECT (); // Get all ratings from different tables $ result = mysql_query ("SELECT COUNT (*) as data 1 to table 1") or die (mysql_error ()); // Check the blank result if (mysql_num_rows ($ result)>) // // looping through all ratings // Rating node $ response ["rating"] = array (); While ($ row = mysql_fetch_array ($ result)) {// temp user array $ rating = array (); $ Rating ["data1"] = $ line ["data1"]; // Enter the single rating in the last response array array_push ($ response ["rating"], $ rating); } $ Result = mysql_query ("SELECT COUNT (*) AS 2 table2 data2") or die (mysql_error ()); While ($ row = mysql_fetch_array ($ result)) {// temp user array $ rating = array (); $ Rating ["data2"] = $ line ["data2"]; // Enter the single rating in the last response array array_push ($ response ["rating"], $ rating); } // resonant JSON response echo json_encode ($ response);

}

I am getting the online JSON output:

  {"Rating": [{"Data 1": "3"}, {"Data2": "4"}]}  

and can confirm that there is no problem with

I currently Referencing 1 piece of data from data 1, but I'm getting an error message for Data 2:

  error parsing data org.json.JSONException: Any data for 2 Value is not  

Here is my JSON code:

  Secure Object Prpostaksaktek (zero v) {// Anil Try Jesan database through data {JSONObject jsonObject = new JSONObject (result); JSONArray jResult = jsonObject.getJSONArray ("Ratings"); For (int i = 0; i & lt; jResult.length (); i ++) {JSONObject object = JRSult.getJSONObject (i); Text_1 = (TextView) findViewById (R.id.txt1); Text_2 = (TextView) findViewById (R.id.txt2); // Get an output on the screen int count1 = object.getInt ("data1"); Text_1.append (count1 + "\ t \ t" + "\ n"); Int count2 = object.getInt ("data2"); Text_2.append (COUNT2 + "\ t \ t" + "\ n"); } This.progressDialog.dismiss (); } Hold (exception e) {// TODO: exception Log.e ("log_tag", "error parsing data" + E.Streatin ()); }}  

I know that I am not skilled in coding, so if anyone has any suggestions to streamline it, I am thankful.

In addition, I am a fan of books to learn, but no books can be found that are available on Android, JSON & amp; MySQL, if any one would like to start, I would be very grateful! Again, the exception is that you have two Jason objects, each of which has 1 data.

(Data 1 or Data 2, not both). So when you request Data 2 on an object without an object, it can not get it. Possible solutions (based on what you are actually looking for):

1) Use optInt to get the value. In this way it will not throw an exception on the missing data.

2) Make sure that instead of an array of server objects, pushing both values ​​into the same object.


Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

c# - Create dynamic LINQ query expression at runtime which translates into a parameterized SQL query for nullable types -

cmake - cpack generates RPMs with %files entries that conflict with the RPM spec. How to fix? -