无法使用android手机调试连接到localhost

时间:2012-07-05 10:10:55

标签: android

我很难从本地主机检索数据。我用xampp。我想要的是显示从localhost到android手机列表视图的数据。这是我在网上找到的关于localhost和android的基本代码。这是片段:

P

ublic class Database_demo extends ListActivity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        String result = null;
        InputStream is = null;
        StringBuilder sb = null;
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        List<String> r = new ArrayList<String>();

        try{

        //http post
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://10.0.2.2/android_connect3/");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
        }
        catch(Exception e){
            Toast.makeText(getBaseContext(),e.toString() ,Toast.LENGTH_LONG).show();
       }

        //Convert response to string  
        try
        {
          BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));

          sb = new StringBuilder();

          String line = null;

          while ((line = reader.readLine()) != null) 
          {
             sb.append(line + "\n");
          }

          is.close();

          result = sb.toString();
        }
        catch(Exception e)
        {
            Toast.makeText(getBaseContext(),e.toString() ,Toast.LENGTH_LONG).show();
        }
        //END Convert response to string   
        try{
                JSONArray jArray = new JSONArray(result);
                JSONObject json_data=null;
                for(int i=0;i<jArray.length();i++)
                {
                   json_data = jArray.getJSONObject(i);
                   r.add(json_data.getString("reg"));
               }
               setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, r));
            }
            catch(JSONException e1){
                Toast.makeText(getBaseContext(),e1.toString() ,Toast.LENGTH_LONG).show();
            } catch (ParseException e1) {
                Toast.makeText(getBaseContext(),e1.toString() ,Toast.LENGTH_LONG).show();
        }

    }
}

这是我的PHP文件:

<?php
mysql_connect('localhost','root','');
mysql_select_db('android_sql');
$sql = "select * from reg";
$query = mysql_query($sql);

while($row = mysql_fetch_assoc($query)){
$output[] = $row;
}
print (json_encode($output));
mysql_close();


?>

这是我的例外:                   http://pastebin.com/QZkfG0bp

2 个答案:

答案 0 :(得分:1)

我认真..我的表现就是这样。

    InputStream is;
    String result;

    StringBuilder sb;
    @Override
    public void onCreate(Bundle savedInstanceState) {

  }

我将它们声明为全局并删除与它们对齐的空值

答案 1 :(得分:0)

你必须检查这一行

HttpPost httppost = new HttpPost("http://10.0.2.2/android_connect3/");

更改为

HttpPost httppost = new HttpPost("http://10.0.2.2/android_connect3/your_php_file_name");