[英]Accessing mySQL on WAMP from Android
我无法理解如何准确配置WAMP,以便Android应用程序对我使用它创建的SQL数据库进行远程(而非LAN)访问。 我知道我不应该直接从应用程序连接到数据库,而应该使用PHP脚本来查询数据库并返回结果。 我不了解的是如何将所有这些组合在一起,如何在哪里放置PHP脚本,如何访问它们,如何允许并连接到Web服务器以从数据库中获取数据。
我已经跟踪并搜索了很多关于此的教程,但是它们似乎都只在本地主机中使用wamp处理,我可以在本地成功访问数据,但是我对如何设置所有内容感到困惑,以便可以从中查询数据库我的android设备上的任何互联网连接。
任何有关如何实现这一目标的建议将不胜感激。
您必须将php脚本上传到服务器:
例如:您的php位于:myserver.com/myscript.php
在您的Android应用中,您发布变量,然后将php脚本与HttpClient连接:
URL_TO_YOUR_SCRIPT = "myserver.com/myscript.php";
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("userId", userId));
HttpClient httpclient = new DefaultHttpClient();
// specify the URL you want to post to
HttpPost httppost = new HttpPost(URL_TO_YOUR_SCRIPT);
try {
// create a list to store HTTP variables and their values
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
HttpResponse response = httpclient.execute(httppost);
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpURLConnection.HTTP_OK) {
HttpEntity getResponseEntity = response.getEntity();
return getResponseEntity.getContent();
} else {
Log.e("ERROR", statusLine.getStatusCode() + "");
HttpEntity getResponseEntity = response.getEntity();
return getResponseEntity.getContent();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
在您的php中:
<?
$userId=$_REQUEST['userId'];
....
// in general you return a json string to get status and result
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.