我正在尝试将我的Android应用程序与后端MySQL连接,我使用php作为服务器端语言,但问题是因为我从php回显$res
数组,所以在Android {{1}中无法访问它}。
我在其他应用程序中尝试了相同的代码并且它在那里工作得很好..但是这段代码没有显示正确的响应,也没有创建Json对象时的错误。
我的用户名在android代码的onPoseExecute(String res)
中定义。
这是我的PHP代码:
OnCrate()
Android代码:
session_start();
$con = mysqli_connect("localhost", "root", "", "SWR");
//Function to sanitize values received from the form. Prevents SQL injection
function clean($str) {
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return $str;
}
$empID = strtoupper(clean($_POST['username']));
$resu = array();
$resu["status"] = true;
$sql3="insert into t_booked_hall values ('','$empID','','','','','','1','','','0','','0','','','','','','','','0','','','0','','')";
$result3 = mysqli_query($con,$sql3);
echo json_encode($resu);
答案 0 :(得分:0)
你必须删除休息;在while循环中
您可以使用getJSONfromURL(url)func从服务器获取阵列响应
public static JSONObject getJSONfromURL(String url) {
InputStream is = null;
String result = "";
JSONObject jArray = null;
// Download JSON data from URL
try {
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 10000);
HttpConnectionParams.setSoTimeout(httpParameters, 5000);
HttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (ConnectTimeoutException e) {
//Here Connection TimeOut excepion
} catch (Exception e) {
}
// Convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "utf-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
}
try {
jArray = new JSONObject(result);
} catch (JSONException e) {
}
return jArray;
}