0

包含以下代码的 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();
?>

检索数据库中所有城市的简单代码。我将此文件位于: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 中的输出 null

我在 android 中调试,这一行抛出异常:(last catch)

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

如果我将其更改为 10.0.2.2 它会在这一行中引发异常:

HttpResponse response = httpclient.execute(httppost);

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

4

2 回答 2

3

第一次向$output其中添加行是未定义的。

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

$output = array();

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

于 2012-05-23T17:45:40.217 回答
1

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

于 2012-05-23T17:27:31.623 回答