dongqieli4164 2010-11-24 19:57
浏览 37
已采纳

Android mysql php问题

I have an application which gets some data from a remote database. I use PHP with the following code to connect to the data base.

mysql_connect($host,$username,$password) or die( "no connection");
@mysql_select_db($database) or die( "Unable to select database");

$query = $_REQUEST['query'];
$q=mysql_query($query);

while($e=mysql_fetch_assoc($q)) {
    $output[]=$e;
}
print(json_encode($output));

mysql_close();

I then connect via following java code

public void connect(ArrayList<NameValuePair> nameValuePairs) {
        result = "";
        InputStream is = null;

        String url = "http://'ipadress'/PhpProject1/EmptyPHP.php";

        //Get the content
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();

            is = httpEntity.getContent();
        } catch (Exception e) {
            Log.e("Connect", "Error in http connection " + e.toString());
        }
        //Convert content toString
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, HTTP.UTF_8), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;

            while ((line = reader.readLine()) != null) {
                sb.append(line + "
");
            }
            is.close();
            result = sb.toString();
            //result = replaceString(sb.toString());
        } catch (Exception e) {
            Log.e("Connect", "Error converting result " + e.toString());
        }
    }

When i have done that I make a query through

public void query(String query){
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("query", query));
        connect(nameValuePairs);
    } 

While this works great with the emulator there is a problem when using it on the phone.

Anyone has a clue why this is?

Thank you in advance

  • 写回答

1条回答 默认 最新

  • dragon012100 2010-11-24 22:05
    关注

    Make sure to connect your real device to the your private network to actually be able to access that server.

    Easiest option would be a WiFi network in the same subnet as the server. Otherwise your phone won't be able to access the network as it is not public.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 luckysheet
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误