1

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.

3
  • 1
    did U add Internet permission in manifest.xml Commented Dec 30, 2011 at 14:30
  • no, I am not added that, how can I do that??? Commented Dec 30, 2011 at 14:35
  • I added that, now my error is org.apache.http.conn.HttpHostConnectException: Connection to http://localhost refused Commented Dec 30, 2011 at 14:39

2 Answers 2

4

in your manifest.xml add this description

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
Sign up to request clarification or add additional context in comments.

5 Comments

I added that, now my error is org.apache.http.conn.HttpHostConnectException: Connection to http://localhost refused
because it doesn't work with localhost or 127.0.0.1 u should change ur local ip adress for exemple 124.0.1.1 or anything else!
or if you can paste your page (index.php) on internet it will work certainly!
How can I change my local ip address? I am now confused and dont get any suggestions from google.
do this and it will let you change/save your hosts file. 1) Browse to Start -> All Programs -> Accessories 2) Right click "Notepad" and select "Run as administrator" 3) Click "Continue" on the UAC prompt 4) Click File -> Open 5) Browse to "C:\Windows\System32\Drivers\etc" 6) Change the file filter drop down box from "Text Documents (.txt)" to "All Files (.*)" 7) Select "hosts" and click "Open" 8) Make the needed changes and close Notepad. Save when prompted.
2

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.

3 Comments

Can u please tell me how can I change my localhost ip address?
He is not saying change your localhost's IP address. He is saying use HttpGet httpPost = new HttpGet("10.0.2.2/simplewebservice/index.php?user=1"); instead of HttpGet httpPost = new HttpGet("localhost/simplewebservice/index.php?user=1"); -- This will connect to your HOST computer (windows / linux / mac) instead of your emulator.
If you were running from tablet onto a local LAN to connect to a server would it be 192.168.1.2/index.php? 192.168.1.2 being the ip address of the server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.