Php 无法从Android中的Json获取正确响应

Php 无法从Android中的Json获取正确响应,php,android,json,Php,Android,Json,我正在制作一个与php脚本交互的系统。php脚本用于向mysqli数据库添加记录。我在用android响应Json时遇到了一个问题。 这是我的android代码 inputStream = httpURLConnection.getInputStream(); bufferedReader= new BufferedReader(new InputStreamReader(inputStream)); String temp;// = null;

我正在制作一个与php脚本交互的系统。php脚本用于向mysqli数据库添加记录。我在用android响应Json时遇到了一个问题。 这是我的android代码

inputStream = httpURLConnection.getInputStream();
        bufferedReader= new BufferedReader(new InputStreamReader(inputStream));
        String temp;// = null;
        StringBuilder stringBuilder  = new StringBuilder();
        while((temp = bufferedReader.readLine())!=null) {
            stringBuilder.append(temp);
        }
        String jsonbuilded = stringBuilder.toString();
        Log.i("jsonbuilded string: ",jsonbuilded);
        JSONObject recievedJsonObject = new JSONObject(jsonbuilded);//Line number 114
        String recievedString = recievedJsonObject.getString("result");
            Log.i("String Recieved",recievedString);
        if(recievedString.compareTo("1")==0){
            runOnthisUIThread("Your query has been successfully placed.");
        }
        else if(recievedString.compareTo("2")==0){
            runOnthisUIThread("The payment of Client "+ params[1] + " is already done by "+params[2]);
        }
        else if(recievedString.compareTo("5")==0){
            Log.i("Entry: ",params[0]);
            runOnthisUIThread("Records not found. Please try again later.");
        }
        else if(recievedString.compareTo("4")==0){
            String employ_assoc = recievedJsonObject.getString("name");
            runOnthisUIThread("The payment is already done by "+ employ_assoc);
        }
        else{
            runOnthisUIThread("Unknown Runtime Error... Kindly contact the Developers.");

        }
runOnthisUIThread()
是一个显示Toast的函数

下面是我的php相关代码片段

if ($query->num_rows >0)
{
  $query_result = $query->fetch_assoc();
  $id = $query_result['ID'];
  if(!$this->con)
  {
    $resultOfQuery['result'] = '0'; //connection not successfully
    die(json_encode(resultOfQuery));
  }
  else
  {
    $queryforcalc = "SELECT * FROM `".$clientid."` ORDER BY id DESC LIMIT 1";
    $queryforcalcresult = $this->con_user->query($queryforcalc);
    if($queryforcalcresult->num_rows >0)
    {
      $query_calc_rawdata = $queryforcalcresult->fetch_assoc();
      //echo $query_calc_rawdata;
      //This section is for calculation of meter readings.
      $lastmeterreadings = $query_calc_rawdata['meter_readings'];
      $meter_readings = $meter_readings - $lastmeterreadings;
    }
        if($meter_readings > 0)
        {
          $timestamp = $this->string->getCurrentTimeStamp();
          $update_user_table = "INSERT INTO `".DATABASE_USER."`.`$clientid` (`timestamp`, `meter_readings`) VALUES ('$timestamp', '$meter_readings');";
          $this->con_user->query($update_user_table);
          //echo $this->con_user->query($update_user_table);
          //echo $this->con->query($update_query);
          $update_query = "UPDATE `".DATABASE."`.`client_list` SET `Meter_readings` = '$meter_readings', `bool` = '1', `Employ_Assoc` = '$employid' WHERE `client_list`.`ID` = '$clientid';";
          //echo $update_query;
          $this->con->query($update_query);
          //echo $this->con;
          $resultOfQuery['result'] = '1'; // successfully placed
          die(json_encode($resultOfQuery));
        }
        else
        {
          $resultOfQuery['result'] = '2';// Invalid entry of meter readings
          die(json_encode($resultOfQuery));
        }

      }
  }
else
{
  $query_statment = "SELECT * FROM client_list WHERE ID = '{$clientid}' AND bool = '1'";
  $queryresultofsecondphase = $this->con->query($query_statment);
  if($queryresultofsecondphase->num_rows >0)
  {
    $queryResult = $queryresultofsecondphase->fetch_assoc();
    $employname = $queryResult['Employ_Assoc'];
    echo $employname;
    $resultOfQuery['result'] = '4';//Record already excists.
    $resultOfQuery['name'] = $employname;
    die(json_encode($resultOfQuery));
  }
  else
  {
    $resultOfQuery['result']= '5';//Record not found. The user does not excists.
    die(json_encode($resultOfQuery));
  }
}
}
当我返回响应“4”时,我还必须发送名称。所以我使用了另一个Json标记“name”。但我得到了一个Json异常。这是我的堆栈跟踪

09-01 12:18:14.097  25321-25357/com.ems.employ I/connection successful﹕ [ 09-01 12:18:14.102 25321:25357 I/jsonbuilded string:  ]
0{"result":"4","name":"0"}
09-01 12:18:14.102  25321-25357/com.ems.employ W/System.err﹕ org.json.JSONException: Value 0 of type java.lang.Integer cannot be converted to JSONObject
09-01 12:18:14.102  25321-25357/com.ems.employ W/System.err﹕ at org.json.JSON.typeMismatch(JSON.java:111)
09-01 12:18:14.102  25321-25357/com.ems.employ W/System.err﹕ at org.json.JSONObject.<init>(JSONObject.java:160)
09-01 12:18:14.102  25321-25357/com.ems.employ W/System.err﹕ at org.json.JSONObject.<init>(JSONObject.java:173)
09-01 12:18:14.102  25321-25357/com.ems.employ W/System.err﹕ at com.ems.employ.authenticatedemploy$SendMeterReadings.doInBackground(authenticatedemploy.java:114)
09-01 12:18:14.102  25321-25357/com.ems.employ W/System.err﹕ at com.ems.employ.authenticatedemploy$SendMeterReadings.doInBackground(authenticatedemploy.java:78)
09-01 12:18:14.097 25321-25357/com.ems.employ I/连接成功﹕ [09-01 12:18:14.102 25321:25357 I/jsonbuilded字符串:]
0{“结果”:“4”,“名称”:“0”}
09-01 12:18:14.102 25321-25357/com.ems.employ W/System.err﹕ org.json.JSONException:无法将java.lang.Integer类型的值0转换为JSONObject
09-01 12:18:14.102 25321-25357/com.ems.employ W/System.err﹕ 在org.json.json.typeMismatch(json.java:111)
09-01 12:18:14.102 25321-25357/com.ems.employ W/System.err﹕ 位于org.json.JSONObject(JSONObject.java:160)
09-01 12:18:14.102 25321-25357/com.ems.employ W/System.err﹕ 位于org.json.JSONObject(JSONObject.java:173)
09-01 12:18:14.102 25321-25357/com.ems.employ W/System.err﹕ 在com.ems.employ.authenticatedemploy$SendMeterReadings.doInBackground(authenticatedemploy.java:114)上
09-01 12:18:14.102 25321-25357/com.ems.employ W/System.err﹕ 在com.ems.employ.authenticatedemploy$SendMeterReadings.doInBackground(authenticatedemploy.java:78)

您的JSON响应看起来是错误的,它的开头是“0”:
0{“result”:“4”,“name”:“0”}
我想这是因为您的php代码中有未注释的
echo
echo$employname


在Android应用程序代码中解析JSON之前,我建议您尝试使用常规浏览器获取JSON,并确保JSON有效,您可以使用此端口。

您的JSON响应看起来错误,它的开头有“0”:
0{“result”:“4”,“name”:“0”}
我认为这是因为您的php代码中有未注释的
echo
echo$employname


在Android应用程序代码中解析JSON之前,我建议您尝试使用常规浏览器获取JSON,并确保JSON有效,您可以使用此端口。

获取
0{“result”:“4”,“name”:“0”}
响应?是的,这是在Android中获取的响应意味着响应无效需要在
0之前删除
0
{
要使其成为有效的json字符串,我该如何做?在响应中获取
0{“结果”:“4”,“名称”:“0”}
?是的,这是在androidmeans中获取的响应,表示响应无效,需要在
{
之前删除
0
,使其成为有效的json字符串,那么我该如何做?