Android Web服务无法解析localhost中的数据

时间:2013-04-05 13:23:25

标签: php android json localhost

我正在尝试将Android App连接到java webservice以获取pHp解析值。

Php脚本是:

<?php
$data = array('name' => 'Froyo', 'version' => 'Android 2.2'); 
print (json_encode($data)); 
?> 

Java WebService是:

package com.json.php;

import android.app.Activity;
import android.os.Bundle;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import android.widget.TextView;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;


public class JSONExampleActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://127.0.0.1/test.php");
        TextView textView = (TextView)findViewById(R.id.textView1);
        try {

            HttpResponse response = httpclient.execute(httppost);
            String jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
            JSONObject object = new JSONObject(jsonResult);

            String name = object.getString("name");
            String verion = object.getString("version");
            textView.setText(name + " - " + verion);

        } 
        catch (JSONException e) {
            e.printStackTrace();
        } 
        catch (ClientProtocolException e) {
            e.printStackTrace();
        } 
        catch (IOException e) {
            e.printStackTrace();
        }


       }
    private StringBuilder inputStreamToString(InputStream is) {
        String rLine = "";
        StringBuilder answer = new StringBuilder();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));

        try {
         while ((rLine = rd.readLine()) != null) {
          answer.append(rLine);
           }
        }

        catch (IOException e) {
            e.printStackTrace();
         }
        return answer;
       }
}

我在HttpPost中尝试了两个http://127.0.0.1/test.php and http://localhost/test.php,它不起作用。 我在浏览器上检查了我的php文件,结果很好。 我哪里错了? 我也在Mac上工作,因为所有权限问题,无论如何都与问题有关?

3 个答案:

答案 0 :(得分:4)

请看一下这页:http://developer.android.com/tools/devices/emulator.html#networkaddresses

你会注意到他们引用:

  • 10.0.2.2作为主机环回接口的特殊别名(即 您的开发机器上的127.0.0.1)
  • 127.0.0.1作为模拟设备自己的环回接口

希望我回答并解决了你的问题:)

答案 1 :(得分:1)

您不应该使用127.0.0.1或localhost,因为Android模拟器在虚拟机(QEMU)内运行,因此127.0.0.1或localhost将是模拟器自己的环回地址。

如果您从Android模拟器中引用系统上的本地主机,则必须使用http://10.0.2.2/。如果Web服务不在您的开发计算机上,则使用您的服务器IP。

如果您的服务器是Windows,请在命令提示符下运行命令“ipconfig”以查找IP地址。

参考:Emulator Networking

答案 2 :(得分:0)

感谢@silentw ..我想出了如何让它发挥作用! 刚刚将HttpPost更改为http://10.0.2.2/test.php