Exception with web service ( java.net.SocketException Permission denied ) in Android

Miya picture Miya · Nov 23, 2010 · Viewed 15.8k times · Source

When I call a Web Service, it returns me this exception: java.net.SocketException Permission denied. I dont know waht is the actual problem. I don't know how to solve this?

home.java page :

     try 
     { 
       url = new URL("http://localhost/TraderLevels/subscriber.php");
             conn = (HttpURLConnection) url.openConnection();
             dis = conn.getInputStream();
     }
     catch (Exception e) 
     {
            e.printStackTrace();

     }

subscriber.php

$username="root";
        $password="";
        $database="mydb";
        $server="localhost";

$connection = mysql_connect($server,$username,$password);

 if (!$connection)
 {
   die('Not connected : ' . mysql_error());
 }

 $db_selected = mysql_select_db($database, $connection);


 if (!$db_selected)
 {
   die('Can\'t use db : ' . mysql_error());
 }


    $query="SELECT * from user";
    $result = mysql_query($query);

    $dom = new DOMDocument('1.0','UTF-8');
    $dnode = $dom->createElement('usesssrdetails');

    $docNode = $dom->appendChild($dnode);

    $result = mysql_query($query);
   $rowNo=1;

  while ($row = @mysql_fetch_assoc($result))
 {

  $node = $dom->createElement('user');
  $categoryNode = $docNode->appendChild($node);

  $idNode = $dom->createElement('userid',($row['userID']));
  $categoryNode->appendChild($idNode);
                $idNode = $dom->createElement('email',($row['email']));
  $categoryNode->appendChild($idNode);


  $rowNo=$rowNo+1;
 }
 $kmlOutput = $dom->saveXML();
 echo $kmlOutput;

?>

Update:

I solved the above problem by adding the below code in the manifest.

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

But I got another exception : java.net.ConnectException: localhost/127.0.0.1:80 - Connection Refused.

Please tell me how to avoid this problem.

Answer

Ramesh picture Ramesh · Feb 24, 2011

http://developer.android.com/guide/appendix/faq/commontasks.html#localhostalias

ifconfig

get the real network address instead of loopback and use that real network adress.