【问题标题】:Url to connect android to mysql database将android连接到mysql数据库的url
【发布时间】:2013-05-08 06:34:07
【问题描述】:

我正在尝试从使用 xampp mysql 创建的 localhost 数据库中检索数据。

public class JASONUseActivity extends Activity {

    EditText byear; // To take birthyear as input from user
    Button submit;
    TextView tv; // TextView to show the result of MySQL query 

    String returnString; // to store the result of MySQL query after decoding JSON

    /** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
            .detectDiskReads().detectDiskWrites().detectNetwork() // StrictMode is most commonly used to catch accidental disk or network access on the application's main thread
        .penaltyLog().build());
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        byear = (EditText) findViewById(R.id.editText1);
        submit = (Button) findViewById(R.id.submitbutton);
        tv = (TextView) findViewById(R.id.showresult);

        // define the action when user clicks on submit button
        submit.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                // declare parameters that are passed to PHP script i.e. the name birth year and its value submitted by user   
                ArrayList < NameValuePair > postParameters = new ArrayList < NameValuePair > ();

                // define the parameter
                postParameters.add(new BasicNameValuePair("birthyear", byear.getText().toString()));
                String response = null;

                // call executeHttpPost method passing necessary parameters 
                try {
                    response = CustomHttpClient.executeHttpPost("http://localhost/jasonscript.php", postParameters);

                    // store the result returned by PHP script that runs MySQL query
                    String result = response.toString();  

我无法在 textview 中查看数据。网址正确吗??

这是位于 xampp/htdocs 中的 jasonscript.php

<?php
$db_host = "localhost";

$db_uid = "root";

$db_pass = "";

$db_name = "";

$db_con = mysql_connect($db_host, $db_uid, $db_pass) or die('could not connect');
mysql_select_db($db_name);
$sql    = "SELECT * FROM people WHERE birthyear > '" . $_POST["birthyear"] . "'";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result))
    $output[] = $row;
print(json_encode($output));
mysql_close();
?>

我已在清单文件中授予互联网权限。

提前致谢。

【问题讨论】:

  • 我假设 Android 中没有错误?你能确认一下(你可能没有得到强制关闭,因为你有一个 try/catch )。如果 Anrdoi 没有错误 - 检查 wamp 服务器是否有错误 - 右键单击​​并转到 apache - 错误日志。如果有任何错误,请运行您的应用并从当时的日志中获取最后几行。
  • 用电脑的IP而不是localhost连接
  • 否,应用程序未强制关闭。 apache 错误日志中没有错误。我在 logcat 中遇到严格模式错误。

标签: php android mysql wamp


【解决方案1】:
设备上的

localhost 指的是它的环回接口,不是您的开发机器正在托管您尝试连接的数据库。为了让虚拟设备连接到您的开发机器的环回接口,您需要使用10.0.2.2。您可以将您的 URL 更改为类似这样的内容以访问您的开发机器:

CustomHttpClient.executeHttpPost("http://10.0.2.2/jasonscript.php", postParameters);

请参阅我的回答 here,了解有关模拟器网络工作原理的更多详细信息。

【讨论】:

    【解决方案2】:

    当您在 Android 设备上使用 localhost 时,您指的是设备本身上的服务。使用您的服务器的 IP / 主机名来获取数据。

    旁注:还要查看ASyncTaskhere,因为在 onCreate 中下载数据会锁定您的 UI 线程(如果需要很长时间)并可能导致 App Not Responding 或 Force Close。即使它没有崩溃,用户体验也会因为应用没有响应而降低。

    【讨论】:

    • 所以,我应该给 "ipaddress/jasonscript.php 或 "ipaddress/localhost/jasonscript.php ?
    • CustomHttpClient.executeHttpPost("http://api.mywebserver.com/jasonscript.php",postParameters); 或 CustomHttpClient.executeHttpPost("http://192.168.0.2/jasonscript.php",postParameters); 当然还有你自己的 IP 地址
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-21
    • 2011-06-10
    • 1970-01-01
    • 2015-09-14
    • 2011-07-28
    • 1970-01-01
    相关资源
    最近更新 更多