org.json.JSONarray类型的值[]无法转换为JSONObject

时间:2015-11-26 16:51:49

标签: php android json

我尝试读取SQL并将其编码回android时,标题中有错误。

我尝试了以下内容:

  1. 通过打印到日志文件验证$ androidIMEI的值,按预期返回值。
  2. 验证了$ sql的输出,并且查询正常(包括来自android的$ _POST [' myIMEI_toString'])值。
  3. 验证了$ json数组的值,当查询从SQL返回2行时,返回2个数组,OK。
  4. 更换

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

    WITH

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

    1. Android(发送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错误:

      org.json.JSONException: Value [] of type org.json.JSONarray cannot be converted to JSONObject
      
    5. (我之前已经问过这个问题,但会再次尝试提供更多信息)

0 个答案:

没有答案