[英]How to connect php and wamp in android
我想在數據庫上有一些數據,我的應用程序從那里檢索數據。 我是初學者,我不知道如何創建數據庫,盡管我已經編寫了PHP腳本和Java代碼來獲得這些結果。 因此,在創建該數據庫時需要我的幫助。 我正在使用WAMP,並且已經創建了一個數據庫。 我的問題是這些調用的參數是什么:
mysql_connect("127.0.0.1","root","xxpasswordxx");
和這個:
public static final String KEY_121 = "http://xx.xx.xxx.xxx/hellomysql/mysqlcon.php";
我在某處讀到我必須將10.0.0.2和我的目錄放置在C:\\ wamp \\ www下的存儲我的php文件的地方。
編輯:添加代碼
這是我的android代碼:
@SuppressWarnings("null")
private void DoMain() {
setContentView(R.layout.main);
// Set the text and call the connect function.
//call the method to run the data retreival
getServerData(KEY_121);
}
public static final String KEY_121 ="http://10.0.2.2:8888/example.php"; //what in here??
private String getServerData(String returnString) {
InputStream is = null;
String result = "";
//the year data to send
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("year","1970"));
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(KEY_121);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//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");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//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")
);
//Get an output to the screen
returnString += "\n\t" + jArray.getJSONObject(i);
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return returnString;
}
這是我的PHP文件
<?php
mysql_connect("127.0.0.1","root","xxpasswordxx"); //what in here??
mysql_select_db("peopledata");
$q=mysql_query("SELECT * FROM people WHERE birthyear>'".$_REQUEST['year']."'");
while($e=mysql_fetch_assoc($q))
$output[]=$e;
print(json_encode($output));
mysql_close();
?>
您需要創建PHP網絡服務才能從android代碼訪問。 在Android端,您需要尋找HttpUrlConnection API。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.