使用PHP从android连接mysql

时间:2012-05-23 17:12:08

标签: php android mysql json

php文件可能存在问题,其中包含以下代码:

    <?php
mysql_connect("localhost","root","");
mysql_select_db("deal");
$sql=mysql_query("select * from CITY");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));
mysql_close();
?>

检索db中所有城市的简单代码。 我将此文件放在:C:\ Program Files \ EasyPHP-5.3.9 \ www \ city.php

java代码非常简单,连接并显示查询结果:

package com.udios.mysql;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.net.ParseException;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class MySQLActivity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void onTestClick(View view) {
        /*Toast.makeText(MySQLActivity.this, "Test Pressed", Toast.LENGTH_SHORT)
                .show();*/

        this.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                String result = null;

                JSONArray jArray;
                InputStream is = null;
                StringBuilder sb = null;
                try {
                    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost("http://127.0.0.1/city.php");
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();
                    is = entity.getContent();

                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(is, "iso-8859-1"), 8);
                    sb = new StringBuilder();
                    sb.append(reader.readLine() + "\n");

                    String line = "0";
                    while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                    is.close();
                    result = sb.toString();

                    int ct_id;
                    String ct_name;
                    Toast.makeText(getBaseContext(), "result is: "+result,
                            100).show();
                    jArray = new JSONArray(result);
                    JSONObject json_data = null;
                    for (int i = 0; i < jArray.length(); i++) {
                        json_data = jArray.getJSONObject(i);
                        ct_id = json_data.getInt("CITY_ID");
                        ct_name = json_data.getString("CITY_NAME");


                        Toast.makeText(getBaseContext(), Integer.toString(ct_id)+" : " +  ct_name , 
                                Toast.LENGTH_LONG).show();

                    }
                } catch (JSONException e1) {
                    Toast.makeText(getBaseContext(), "No City Found",
                            Toast.LENGTH_LONG).show();
                } catch (ParseException e1) {
                    e1.printStackTrace();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }

            }
        });

    }
}

当我尝试直接在浏览器中访问:

我得到: 注意:未定义的变量:第7行的C:\ Program Files \ EasyPHP-5.3.9 \ www \ city.php中的输出 空

我在android中进行调试,此行抛出异常:(最后一次捕获)

HttpPost httppost = new HttpPost("http://127.0.0.1/city.php");

如果我将其更改为10.0.2.2,则会在此行中抛出异常:

HttpResponse response = httpclient.execute(httppost);

我真的尝试了所有组合,有人可以帮助我吗? 非常感谢你 UDI

2 个答案:

答案 0 :(得分:3)

第一次向$output添加行时,未定义。

在PHP代码中的while循环之前添加它。

$output = array();

如果您在模拟器上运行,则还需要连接10.0.2.2,或者在真实设备上运行本地IP(确保设备通过WiFi连接到您的网络)。

答案 1 :(得分:1)

您的HttpPost正在尝试连接正在运行的手机。它需要指向运行服务器的计算机。 127.0.0.1 =此设备。