Get xml from a php webservice url using android

半世苍凉 提交于 2019-12-18 09:23:47

问题


I want to read a xml from a service url, I wrote the code, my url is ok, seen from browser,

    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;
}

But it gives me the following error java.net.SocketException: Permission denied. Can anyone have a better solution for this?

Kind regards,

Pritom.


回答1:


I am sure that your using emulator.

Andriod emulator is separate virtual machine by itself. If we provide localhost/127.0.0.1 as a hostname, then emulator will try to search url within its environment. To avoid this problem, we need to provide the ipaddress of local machine.

Pls note that machine name as a hostname will also give problem.

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

Use 10.0.2.2 to connect to your host machine.



回答2:


in your manifest.xml add this description

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


来源:https://stackoverflow.com/questions/8680457/get-xml-from-a-php-webservice-url-using-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!