[英]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.