Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将android连接到mysql数据库的Url_Php_Android_Mysql_Wamp - Fatal编程技术网

Php 将android连接到mysql数据库的Url

Php 将android连接到mysql数据库的Url,php,android,mysql,wamp,Php,Android,Mysql,Wamp,我正在尝试从使用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 return

我正在尝试从使用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();  
public类JASONUseActivity扩展活动{
EditText byear;//从用户获取生日作为输入
按钮提交;
TextView tv;//TextView显示MySQL查询结果
String returnString;//解码JSON后存储MySQL查询结果
/**在首次创建活动时调用*/
@凌驾
创建时受保护的void(Bundle savedInstanceState){
StrictMode.setThreadPolicy(新的StrictMode.ThreadPolicy.Builder()
.detectDiskReads().detectDiskWrites().detectNetwork()//StrictMode最常用于捕获应用程序主线程上的意外磁盘或网络访问
.penaltyLog().build());
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
byear=(EditText)findViewById(R.id.editText1);
提交=(按钮)findViewById(R.id.submitbutton);
tv=(文本视图)findViewById(R.id.showresult);
//定义用户单击提交按钮时的操作
submit.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
//声明传递给PHP脚本的参数,即名称出生年份及其由用户提交的值
ArrayListpostParameters=新的ArrayList();
//定义参数
add(新的BasicNameValuePair(“生日”,byear.getText().toString());
字符串响应=null;
//调用executehttpost方法传递必要的参数
试一试{
响应=CustomHttpClient.executeHttpPost(“http://localhost/jasonscript.php“,后参数);
//存储运行MySQL查询的PHP脚本返回的结果
字符串结果=response.toString();
我无法在textview中查看数据。url是否正确

下面是位于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设备上使用
localhost
时,您是在设备本身上引用服务。使用服务器的IP/主机名获取数据


旁注:还要考虑
ASyncTask
,因为在onCreate中下载数据会锁定UI线程(如果需要很长时间)并且可能会导致
应用程序没有响应
强制关闭
。即使它没有崩溃,用户体验也会降低,因为应用程序没有响应。

设备上的本地主机
指的是它的环回接口,不是承载您正在尝试的数据库的开发机器正在初始化以连接到。为了让虚拟设备连接到开发计算机的环回接口,您需要使用
10.0.2.2
。您可以将URL更改为类似以下内容以访问您的开发计算机:

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

有关仿真器网络工作原理的更多详细信息,请参见我的回答。

我假设Android中没有错误?您能否确认(您可能没有强制关闭,因为您有一个try/catch)。如果Anrdoi没有错误-检查wamp服务器是否有错误-右键单击并转到apache-错误日志。如果有错误,请运行应用程序并从日志中获取当时的最后一行。连接计算机的IP而不是本地主机否应用程序没有强制关闭。apache错误日志中没有错误。我正在使用严格模式logcat中出现错误。因此,我应该给出“ipaddress/jasonscript.php”或“ipaddress/localhost/jasonscript.php”
CustomHttpClient.executeHttpPost(“http://api.mywebserver.com/jasonscript.php“,后参数);
自定义HttpClient.ExecuteHttpOppost(”http://192.168.0.2/jasonscript.php“,后参数);
当然有您自己的IP地址