0

我正在尝试这段代码,但它给出了 null。它应该在哪里给我地名。我的代码中似乎有什么问题?我想通过提供地点 ID 来获取地点详细信息。但我也调试过,它总是返回 null

代码

String result = "";
                InputStream is = null;
                // the year data to send
                ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("place_id", "2"));

                // http post
                try {
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost(
                            "http://example.com/getAllPeopleBornAfter.php");
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();
                    is = entity.getContent();
                } catch (Exception e) {
                    Log.e("log_tag", "Error in http connection " + e.toString());
                }
                // convert response to string
                try {
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(is, "iso-8859-1"), 8);
                    StringBuilder sb = new StringBuilder();
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                    is.close();

                    result = sb.toString();
                } catch (Exception e) {
                    Log.e("log_tag", "Error converting result " + e.toString());
                }

                // parse json data
                try {
                    JSONArray jArray = new JSONArray(result);
                    for (int i = 0; i < jArray.length(); i++) {
                        JSONObject json_data = jArray.getJSONObject(i);
                        TextView z = (TextView)findViewById(R.id.textView1);
                        z.setText(json_data.getString("place_id"));
                    }
                } catch (JSONException e) {
                    Log.e("log_tag", "Error parsing data " + e.toString());
                }
            }

        });

PHP

<?php
include "db_config.php";

$q=mysql_query("SELECT 'name' FROM places WHERE place_id='".$_REQUEST['place_id']."'");

while($e=mysql_fetch_assoc($q))
        $output[]=$e;

print(json_encode($output));

mysql_close();
?>
4

2 回答 2

0

这就是答案

ref.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                String result = "";
                InputStream is = null;

                ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("place_id", "3"));

                try {
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost(
                            "http://hopscriber.com/test.php");
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();
                    is = entity.getContent();

                } catch (Exception e) {
                    Log.e("log_tag", "Error in http connection " + e.toString());
                }
                // convert response to string
                try {
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(is, "iso-8859-1"), 8);
                    StringBuilder sb = new StringBuilder();
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                    is.close();
                    result = sb.toString();
                } catch (Exception e) {
                    Log.e("log_tag", "Error converting result " + e.toString());
                }

                // parse json data
                String version = null;
                try {
                    JSONArray jArray = new JSONArray(result);
                    for (int i = 0; i < jArray.length(); i++) {
                        JSONObject json_data = jArray.getJSONObject(i);
                        version = json_data.getString("name");
                        Toast.makeText(getBaseContext(), version,
                                Toast.LENGTH_LONG).show();
                        TextView x = (TextView) findViewById(R.id.textView1);
                        x.setText(version);
                    }
                } catch (JSONException e1) {
                    Toast.makeText(getBaseContext(), version, Toast.LENGTH_LONG)
                            .show();
                } catch (ParseException e1) {
                    e1.printStackTrace();
                }
            }

        });

php

<?php

include "db_config.php";

$query = mysql_query("SELECT * FROM places WHERE place_id='".mysql_real_escape_string($_POST[place_id])."'");

while($e=mysql_fetch_assoc($query))
    $output[]=$e;

print(json_encode($output));

mysql_close();
?>

谢谢大家的帮助

于 2012-07-20T15:18:50.160 回答
0

信息太少,无法提供帮助。您确定您的 PHP 脚本返回任何内容吗?http响应的日志记录显示了什么?也许您的代码一切都很好,而 null 是您应该得到的,因为问题潜伏在其他地方?

顺便说一句:您的 PHP 代码非常糟糕。您对 SQL 注入的利用持开放态度,一般来说,在尝试使用它应该返回的预期数据之前,您应该始终检查查询(或 fopen 或任何东西)是否成功,因为不能保证一切正常。错误检查的习惯对于可靠的软件开发至关重要。我还建议不要使用“?>”,如果它只是纯 PHP 脚本文件,而不是与 HTML 或任何东西混合(混合也很糟糕)。

于 2012-07-20T14:35:33.767 回答