将Android应用程序连接到PHP mySQL

时间:2015-04-14 01:13:28

标签: php android mysql

我正在开发一个Android应用程序,在其中我从任何浏览器(甚至在智能手机中)使用PHP文件获取信息并将其称为:

http://myapp.server.com/getPackages.php

问题在于,当我在Android Studio上运行它时,我可以获得所有信息,但是当我在手机上运行它时(我正在复制调试APK并安装它)我无法与服务器建立任何连接。 / p>

注意:我的Manifest已拥有互联网权限。

例如,这是一种用于从数据库中检索信息的方法:

public String getAllDestinations(Context contexto){
InputStream is = null ;
String result = "";
String url_select = "http://turutaapp.byethost9.com/getDestinations.php";
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url_select);

ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();

try {
    httpPost.setEntity(new UrlEncodedFormEntity(param));

    HttpResponse httpResponse = httpClient.execute(httpPost);
    HttpEntity httpEntity = httpResponse.getEntity();

    //read content
    is =  httpEntity.getContent();

} catch (Exception e) {
    showMsg("Error1",contexto);
    Log.e("log_tag", "Error in http connection " + e.toString());
}

try {
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    String line = "";
    while((line=br.readLine())!=null)
    {
        sb.append(line+"\n");
    }
    is.close();
    result=sb.toString();
} catch (Exception e) {
    Log.e("log_tag", "Error converting result "+e.toString());
}

return result;

}

这是我用来获取该信息的php文件:

<?php
mysql_connect("server","database","pass") or  die(mysql_error());
mysql_select_db("b9_16044321_tuRutaApp");
$sql=mysql_query(" SELECT DestinationId,Name,NumPlacesAvailable,Price FROM        Destinations Dest WHERE Dest.Enable = 1 ");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));
mysql_close();
?>

0 个答案:

没有答案