I have an error as stated in the title when trying to read SQL and encode it back to android.
I have tried the following:
- Verified the value of $androidIMEI by printing to log file, value is returned as expected.
- Verified the output of $sql and the query is OK (including the $_POST['myIMEI_toString']) value from android.
- Verified the value of $json array, 2 arrays are returned as the query returns 2 rows from SQL, OK.
Replacing
$androidIMEI = isset($_POST['myIMEI_toString']) ? $_POST['myIMEI_toString'] : '';
WITH
$androidIMEI = "000000000000000" //works fine but I want to get that programmatically.
Code:
Android (Send IMEI):
TelephonyManager mngr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); myIMEI = mngr.getDeviceId(); myIMEI_toString = myIMEI.toString();
...............
protected String doInBackground(String... arg0) { ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("myIMEI_toString",myIMEI_toString)); try { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://path_on_server/file.php"); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); InputStreamReader ireader = new InputStreamReader(is); BufferedReader bf = new BufferedReader(ireader); sb = new StringBuilder(); String line = null; while ((line = bf.readLine()) != null) { sb.append(line); } Log.e("pass 1", "connection success "); } catch(Exception e) { System.out.println("Error catch"); } return id; }
Android (JSON):
try { JSONObject jsonResponse = new JSONObject(jsonResult); JSONArray jsonMainNode = jsonResponse.optJSONArray("myarray"); for (int i = 0; i < jsonMainNode.length(); i++) { JSONObject jsonChildNode = jsonMainNode.getJSONObject(i); name = jsonChildNode.getString("Request_Name"); number = jsonChildNode.getString("Request_Number"); username = jsonChildNode.getString("Request_Username"); status = jsonChildNode.getString("Status"); arrName.add(name); arrNumber.add(number); arrUsername.add(username); arrStatus.add(status); System.out.println("Name: "+name); System.out.println("Number: "+number); System.out.println("Username: "+username); System.out.println("Status: "+status); } } catch (JSONException e) { Log.i("Error Log: ", e.toString()); System.out.println("Error: "+e.toString()); Toast.makeText(getApplicationContext(), "Error" + e.toString(), Toast.LENGTH_SHORT).show(); }
PHP:
<?php include 'config.php'; //$androidIMEI = "000000000000000"; $androidIMEI = isset($_POST['myIMEI_toString']) ? $_POST['myIMEI_toString'] : ''; //$f = fopen("log.txt", "w"); //fwrite($f, print_r($androidIMEI, true)); //fclose($f); $con=mysql_connect("$servername", "$username", "$password")or die("cannot connect"); mysql_select_db("$dbname")or die("cannot select DB"); $sql = "SELECT * from users WHERE Request='0' AND IMEI = '$androidIMEI' "; $result = mysql_query($sql); $json = array(); if(mysql_num_rows($result)){ while($row=mysql_fetch_assoc($result)){ $json['myarray'][]=$row; } } else { //$error = "Error selecting record: " . $conn->error " "; //$f = fopen("$error.txt", "w"); //fwrite($f, print_r($error, true)); //fclose($f); } $f = fopen("log.txt", "w"); fwrite($f, print_r($sql, true)); fclose($f); mysql_close($con); echo json_encode($json); ?>
Logcat Error:
org.json.JSONException: Value [] of type org.json.JSONarray cannot be converted to JSONObject
(i have asked this question before but will try again with more information)