将android应用程序连接到mysql数据库
我一直在试用各种网站上显示的使用php连接到MySQL数据库的教程。我不知道下面的代码有什么问题。任何人都可以告诉我我需要做什么。将android应用程序连接到mysql数据库
这是我的PHP代码
<?php
mysql_connect("localhost","root","sugi");
mysql_select_db("android");
$q=mysql_query("SELECT * FROM people
WHERE
birthyear>'".$_REQUEST['year']."'");
while($e=mysql_fetch_assoc($q))
$output[]=$e;
print(json_encode($output));
mysql_close(); ?>
这是我的SQL查询
CREATE TABLE `people` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR(100) NOT NULL ,
`sex` BOOL NOT NULL DEFAULT '1',
`birthyear` INT NOT NULL
)
这是Android
public class main extends Activity {
InputStream is;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String result = "";
//the year data to send
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("year","1990"));
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost/index.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("log_tag", "connection success ");
Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show();
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show();
}
//parse json data
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag","id: "+json_data.getInt("id")+
", name: "+json_data.getString("name")+
", sex: "+json_data.getInt("sex")+
", birthyear: "+json_data.getInt("birthyear")
);
Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show();
}
}
}
程序做工精细我的Java代码。但我不能连接到http://localhost/index.php
。程序显示失败3次。可以帮我看看我哪里出错了?
感谢大家的帮助。现在我可以连接到MySQL。但我不能得到json数据的价值。编制敬酒信息2通和1失败。谁能帮我?下面的图片是当我在我的IE中键入http://localhost/index.php
。而第6行是这一切
$q=mysql_query("SELECT * FROM people WHERE birthyear>'".$_REQUEST['year']."'");
我不知道我哪里出错了。
尝试http://127.0.0.1/index.php以代替localhost
127.0.0.1是localhost的同义词。无论哪种方式,它指向自己(电话),它永远不会到达,并指向其他计算机,如果它指向自己。如果mysql服务器和web服务器直接托管在手机上,这将工作。 – 2011-04-03 16:21:00
在我看来,他和模拟器一起工作,我在使用本地主机时遇到了问题,并且当我更改为127.0.0.1时它工作正常。 – James 2011-04-03 16:26:12
不是我的观点,webserver/mysql不在本地主机上。即使在模拟器localhost仍然是电话而不是主机。 – 2011-04-03 21:35:56
如果你的PHP脚本部署在本地主机,要部署在模拟器Android应用,那么你应该使用此构造函数: HttpPost httppost =新HttpPost(” http://10.0.2.2/index.php“);
请参见:http://developer.android.com/guide/developing/devices/emulator.html#emulatornetworking
的问题是,你要在本地主机上访问的东西。代码运行时,本地主机将成为您的手机。如果你使用模拟器,你可以使用Andrey的解决方案。
否则,如果您在本地网络上使用WiFi,您必须知道您的计算机的IP地址。或者如果你使用3G,Edge,GSM网络。你应该输入你的电脑的IP地址。
你可以去那里:http://whatismyip.com
如果您使用路由器或ISP阻止HTTP端口(80)。你必须做一个端口重定向到不同的东西。我的猜测是你应该使用手机和网页浏览器测试网址。
HttpPost httppost = new HttpPost("http://localhost/index.php");
将其更改为您的本地主机的IP地址。
我对android应用程序很新颖,但是我知道如果您将您的帖子更改为http://192.168.x.x(您的本地本地IP地址在您的路由器中),您应该可以查看它。我运行一个WAMP环境,然后在从我的网络中的其他计算机访问我的开发计算机本地主机时遇到问题。我所做的只是确保我的WAMP服务器在线。如果你点击WAMP图标,你会看到“放入线上”或“放下线”。然后,我可以通过http://192.168.x.x/android/index.php在我的本地网络中的其他笔记本电脑上查看我的网站。
我对PHP一无所知,但是我发现你的请求有正确的答案:JSON对象数组。可能PHP脚本工作正常。 – 2011-04-05 07:22:40