【问题标题】:Get xml from a php webservice url using android使用 android 从 php webservice url 获取 xml
【发布时间】:2011-12-30 22:43:14
【问题描述】:

我想从一个服务url读取一个xml,我写了代码,我的url没问题,从浏览器看,

    public String getXML(){
    String line = null;
    try {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet httpPost = new HttpGet("http://localhost/simplewebservice/index.php?user=1");

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        line = EntityUtils.toString(httpEntity);
    } catch (Exception ex) {
        Log.d("Error reading xml", ex.toString());
    }
    return line;
}

但它给了我以下错误java.net.SocketException: Permission denied。 谁能有更好的解决方案?

亲切的问候,

Pritom。

【问题讨论】:

  • 你是否在 manifest.xml 中添加了 Internet 权限
  • 不,我没有添加那个,我该怎么做???
  • 我补充说,现在我的错误是org.apache.http.conn.HttpHostConnectException: Connection to http://localhost refused

标签: android xml http-get


【解决方案1】:

在你的 manifest.xml 中添加这个描述

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

【讨论】:

  • 我补充说,现在我的错误是org.apache.http.conn.HttpHostConnectException: Connection to http://localhost refused
  • 因为它不适用于 localhost 或 127.0.0.1 您应该更改您的本地 IP 地址,例如 124.0.1.1 或其他任何东西!
  • 或者如果您可以将您的页面 (index.php) 粘贴到互联网上,它肯定会起作用!
  • 如何更改我的本地 IP 地址?我现在很困惑,没有得到谷歌的任何建议。
  • 这样做,它会让你更改/保存你的主机文件。 1) 浏览开始 -> 所有程序 -> 附件 2) 右键单击​​“记事本”并选择“以管理员身份运行” 3) 在 UAC 提示符上单击“继续” 4) 单击文件 -> 打开 5) 浏览到“C: \Windows\System32\Drivers\etc" 6) 将文件过滤器下拉框从 "Text Documents (.txt)" 更改为 "All Files (.*)" 7) 选择 "hosts" 并单击“打开” 8) 进行所需的更改并关闭记事本。提示时保存。
【解决方案2】:

我确定您使用的是模拟器。

Andriod 模拟器本身就是独立的虚拟机。如果我们提供localhost/127.0.0.1 作为主机名,那么模拟器将尝试在其环境中搜索 url。为了避免这个问题,我们需要提供本地机器的 ipaddress。

请注意,机器名作为主机名也会出现问题。

127.0.0.1 refers to localhost in the Emulator, not your machine.

Use 10.0.2.2 to connect to your host machine.

【讨论】:

  • 你能告诉我如何更改我的 localhost ip 地址吗?
  • 他不是说改变你的本地主机的IP地址。他说使用 HttpGet httpPost = new HttpGet("10.0.2.2/simplewebservice/index.php?user=1"); 而不是 HttpGet httpPost = new HttpGet("localhost/simplewebservice/index.php?user=1"); -- 这将连接到你的主机计算机(windows / linux / mac)而不是你的模拟器。
  • 如果您从平板电脑运行到本地 LAN 以连接到服务器,它会是 192.168.1.2/index.php 吗? 192.168.1.2 是服务器的ip地址。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-05
  • 2019-12-06
  • 2017-08-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多