无法使用PHP从Android连接到MySQL
我正试图通过以下教程从android设备连接到MySQL: 所发生的一切是,它正在打印我希望在模拟器上连接到的服务器的ip地址。它在logcat中显示以下错误:无法使用PHP从Android连接到MySQL,php,android,mysql,Php,Android,Mysql,我正试图通过以下教程从android设备连接到MySQL: 所发生的一切是,它正在打印我希望在模拟器上连接到的服务器的ip地址。它在logcat中显示以下错误: 12-08 11:42:01.993: I/dalvikvm(274): threadid=3: reacting to signal 3 12-08 11:42:02.113: I/dalvikvm(274): Wrote stack traces to '/data/anr/traces.txt' 12-08 11:42
12-08 11:42:01.993: I/dalvikvm(274): threadid=3: reacting to signal 3
12-08 11:42:02.113: I/dalvikvm(274): Wrote stack traces to '/data/anr/traces.txt'
12-08 11:42:03.273: E/log_tag(274): Error parsing data org.json.JSONException: Value
<!DOCTYPE of type java.lang.String cannot be converted to JSONArray
12-08 11:45:58.283: E/log_tag(351): Error parsing data org.json.JSONException: Value
<!DOCTYPE of type java.lang.String cannot be converted to JSONArray
12-08 11:53:11.302: E/log_tag(378): Error parsing data org.json.JSONException: Value
<!DOCTYPE of type java.lang.String cannot be converted to JSONObject
12-08 12:03:31.643: E/log_tag(405): Error parsing data org.json.JSONException: Value
<!DOCTYPE of type java.lang.String cannot be converted to JSONArray
12-08 12:20:57.052: E/log_tag(432): Error parsing data org.json.JSONException: Value
<!DOCTYPE of type java.lang.String cannot be converted to JSONArray
12-08 11:42:01.993:I/dalvikvm(274):threadid=3:对信号3作出反应
12-08 11:42:02.113:I/dalvikvm(274):将堆栈跟踪写入“/data/anr/traces.txt”
12-08 11:42:03.273:E/log_标记(274):解析数据org.json.JSONException:Value时出错
有人能解释一下吗?我快发疯了
感谢您在浏览器上打开该页面(employee.php),查看源代码(在大多数浏览器上为CTRL+U),并确保只返回JSON字符串,因为它显然无法将结果解析为JSON数组。如果我在浏览器中访问,我会收到404错误。这是因为您的PHP脚本设置不正确,或者不是您认为的位置
你需要做两件事:
在Java中,检查请求的响应代码。您可以这样做:
// ...
HttpResponse response = httpclient.execute(httppost);
StatusLine responseStatus = response.getStatusLine();
if (responseStatus.getStatusCode() != 200) {
// Handle error here
} else {
HttpEntity entity = response.getEntity();
// ...
在PHP中,您需要处理潜在的错误:
<?php
// Database connection settings
$dbHost = '86.47.59.249';
$dbUser = 'username';
$dbPass = 'password';
$dbName = 'Test';
// Try and connect to the database
if (!mysql_connect($dbHost, $dbUser, $dbPass)) {
header('HTTP/1.1 500 Internal Server Error');
exit('Oh No! Something went wrong connecting to the database: '.mysql_error());
} else if (!mysql_select_db($dbName)) {
header('HTTP/1.1 500 Internal Server Error');
exit('Oh No! Something went wrong selecting the database: '.mysql_error());
}
// Define SQL query
$query = "SELECT *
FROM Tbl_Employee
WHERE EmployeeId > '".mysql_real_escape_string($_REQUEST['code'])."'";
// Try and execute the query
if (!$result = mysql_query($query)) {
header('HTTP/1.1 500 Internal Server Error');
exit('Oh No! Something went wrong with the query: '.mysql_error());
}
// Fetch all results into an array
while ($row = mysql_fetch_assoc($result)) {
$output[] = $e;
}
// Close database link
// You can safely leave this line out, PHP implicitly does this when
// it terminates
mysql_close();
// Exit with a JSON encoded string of the results
exit(json_encode($output));
使用firefox和firebug扩展来查看php脚本返回的数据。您很可能没有使用正确的JSON.get*函数。您应该立即更改php脚本-将$\u REQUEST['code']
更改为mysql\u real\u escape\u string($\u REQUEST['code'])
。如果不做此更改,阅读本文的任何人都有足够的信息,可以轻松地使用SQL注入危害您的数据库和服务器。请参阅本页的背景阅读:感谢刚刚更改了,当您输入您无法访问的原因时,它是因为这不是我使用的实际地址。但是,当我输入真实地址时,会出现404错误,但是如果我使用http://I的ftp://intead输入地址,我可以连接。有没有办法将ftp合并到我的php文件中?@davecm ftp服务器不会调用(运行)php脚本并向您提供输出,它只会向您提供php源代码。您需要让PHP脚本在web服务器上运行。如果它是一个有效的web服务器,我想这可能只是将脚本移动到正确的目录以允许它作为网页使用的问题。好的,我刚刚发现我连接到了错误的服务器。原来是我的电脑托管了MySQLServer。我现在有点困惑,因为我不认为这是一个网络服务器。当我运行localhost\employee.php时,它只给我打开或保存文件的选项。有什么想法吗?听起来您的机器上没有安装PHP,或者web服务器没有正确设置为使用PHP处理.PHP文件。您是否有一个合适的支持PHP的web服务器可以使用?没有。我在这台机器上唯一的web服务器是Internet信息服务。你推荐我用什么?很抱歉,这件事伤了你的脑筋!
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.david.Connect"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".ConnectActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>
// ...
HttpResponse response = httpclient.execute(httppost);
StatusLine responseStatus = response.getStatusLine();
if (responseStatus.getStatusCode() != 200) {
// Handle error here
} else {
HttpEntity entity = response.getEntity();
// ...
<?php
// Database connection settings
$dbHost = '86.47.59.249';
$dbUser = 'username';
$dbPass = 'password';
$dbName = 'Test';
// Try and connect to the database
if (!mysql_connect($dbHost, $dbUser, $dbPass)) {
header('HTTP/1.1 500 Internal Server Error');
exit('Oh No! Something went wrong connecting to the database: '.mysql_error());
} else if (!mysql_select_db($dbName)) {
header('HTTP/1.1 500 Internal Server Error');
exit('Oh No! Something went wrong selecting the database: '.mysql_error());
}
// Define SQL query
$query = "SELECT *
FROM Tbl_Employee
WHERE EmployeeId > '".mysql_real_escape_string($_REQUEST['code'])."'";
// Try and execute the query
if (!$result = mysql_query($query)) {
header('HTTP/1.1 500 Internal Server Error');
exit('Oh No! Something went wrong with the query: '.mysql_error());
}
// Fetch all results into an array
while ($row = mysql_fetch_assoc($result)) {
$output[] = $e;
}
// Close database link
// You can safely leave this line out, PHP implicitly does this when
// it terminates
mysql_close();
// Exit with a JSON encoded string of the results
exit(json_encode($output));