0

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:

  1. Verified the value of $androidIMEI by printing to log file, value is returned as expected.
  2. Verified the output of $sql and the query is OK (including the $_POST['myIMEI_toString']) value from android.
  3. Verified the value of $json array, 2 arrays are returned as the query returns 2 rows from SQL, OK.
  4. Replacing

    $androidIMEI = isset($_POST['myIMEI_toString']) ? $_POST['myIMEI_toString'] : '';
    

    WITH

    $androidIMEI = "000000000000000" //works fine but I want to get that programmatically.
    

Code:

  1. 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;     
    
            }
    
  2. 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();
      }
    
  3. 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); 
    ?> 
    
  4. 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)

user3560827
  • 632
  • 1
  • 5
  • 22
  • Doesn't it tell you what line the error is on? – Jonnix Nov 26 '15 at 17:00
  • You are returning an array parsed in JSON (JSONarray) and you are expecting a JSONObject. Take a look at this question: http://stackoverflow.com/questions/12289844/difference-between-jsonobject-and-jsonarray – jolmos Nov 26 '15 at 17:04

0 Answers0