2014-11-25 46 views
0

嗨,我很新的Android客户端php服务器。我按照JSON的一些教程发布帖子和回复变量,但是这个回应是错误Value of type java.lang.String cannot be converted to JSONObjectphp服务器解析简单的字符串使用JSON到Android客户端

JSON后是成功,但回应是错误的。

的Android代码:

HttpConnectionParams.setConnectionTimeout(httpParameters, 15000); 
HttpConnectionParams.setSoTimeout(httpParameters, 15000);   

HttpClient httpclient = new DefaultHttpClient(httpParameters); 
HttpPost httppost = new HttpPost("http://192.168.1.1/databastest/login.php"); 
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));   
HttpResponse response = httpclient.execute(httppost); 
HttpEntity entity = response.getEntity(); 

String result = EntityUtils.toString(entity); 

// Create a JSON object from the request response 
JSONObject jsonObject = new JSONObject(result); 

//Retrieve the data from the JSON object 
resultLoging = jsonObject.getString("ResultArray"); 


}catch (Exception e){ 
    Log.e("ClientServerDemo", "Error:", e); 
    exception = e; 
} 

return true; 
} 

@Override 
protected void onPostExecute(Boolean valid){ 
    //Update the UI 
    Toast.makeText(mContext, resultLoging, Toast.LENGTH_LONG).show(); 
    if(exception != null){ 
     Log.i("Error",exception.getMessage()); 
     Toast.makeText(mContext, exception.getMessage(), Toast.LENGTH_LONG).show(); 
     } 
    } 

PHP代码

mysqli_query($con,"INSERT INTO usersacc 
(phone, password) VALUES('$pho', '$pass')"); 


     #Build the result array (Assign keys to the values) 
     $result_data = array( 
      'ResultArray' => 'success', 
      ); 

     #Output the JSON data 
     echo json_encode($result_data); 

插入成功,但结果却没有这样做。

+0

尝试打印“的JSONObject”的数据,找出数据格式。 – 2014-11-25 06:07:11

回答

0

将此代码替换为此可能工作。获取从服务器作为对象而不是字符串的响应。

Object result = EntityUtils.toString(entity); 
JSONObject jsonObject = new JSONObject(result.toString()); 
resultLoging = jsonObject.getString("ResultArray"); 
+0

对不起仍然没有工作:( – 2014-11-25 06:18:59

+0

尝试打印“jsonObject”数据,是在json格式? – 2014-11-25 06:20:02

+0

Log.i(“jsonformabc”,jsonObject.toString()); 我用这个但没有回应?? – 2014-11-25 06:27:04

相关问题