[英]connecting android apps to mysql database
我一直在尝试在各种网站上显示的教程,使用php连接MySQL数据库到android。 我不知道下面的代码有什么问题。 谁能告诉我我需要做什么。
这是我的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中的java代码
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();
}
}
}
该计划工作正常。 但我无法连接到http://localhost/index.php
。 程序显示失败3次。 可以帮我看看我哪里出错了?
感谢大家的帮助。 现在我能够连接到mysql。 但我无法获得json数据的价值。 编程传播一个msg 2传递,1个失败。 谁能帮我? 下图是我在IE中输入http://localhost/index.php
。 而第6行就是这一切
$q=mysql_query("SELECT * FROM people WHERE birthyear>'".$_REQUEST['year']."'");
我不知道我哪里出错了。
如果您的php脚本部署在localhost并且您正在模拟器上部署您的Android应用程序,那么您应该使用此构造函数:HttpPost httppost = new HttpPost(“http://10.0.2.2/index.php”);
请参阅: http : //developer.android.com/guide/developing/devices/emulator.html#emulatornetworking
HttpPost httppost = new HttpPost("http://localhost/index.php");
将其更改为localhost的IP地址。
我对Android应用程序很新,但我知道如果你将你的帖子更改为http://192.168.xx (你的路由器中的本地主机本地IP地址),你应该能够很好地查看它。 我运行WAMP环境,然后在从网络中的其他计算机访问我的开发计算机localhost时遇到问题。 我所做的只是确保我的WAMP服务器在线。 如果单击WAMP图标,您将看到“Put Online”或“Put Offline”。 然后我可以通过http://192.168.xx/android/index.php在我的本地网络中的其他笔记本电脑上查看我的网站就好了。
问题是你正试图访问localhost上的东西。 代码运行时,Localhost将成为您的手机。 如果您使用模拟器,您可以使用Andrey的解决方案。
否则,如果您在本地网络上使用wifi,则必须知道计算机的IP地址。 或者如果您使用3G,Edge,GSM互联网。 您应该输入计算机的IP。
你可以去那里: http : //whatismyip.com
如果您使用路由器或您的isp阻止http端口(80)。 您必须将端口重定向到不同的端口。 我的猜测是你应该使用手机和网络浏览器测试网址。
尝试http://127.0.0.1/index.php而不是localhost
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.