嗨,我很新的Android客户端php
服务器。我按照JSON
的一些教程发布帖子和回复变量,但是这个回应是错误。 Value of type java.lang.String cannot be converted to JSONObject
。php服务器解析简单的字符串使用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);
插入成功,但结果却没有这样做。
尝试打印“的JSONObject”的数据,找出数据格式。 – 2014-11-25 06:07:11