Exception with web service ( java.net.SocketException Permission denied ) in Android
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";
$开发者_如何学Goserver="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.
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.
Are you actually running a webserver on localhost?
If so, have you verified it's listening with netstat and actually tested it with a known-working client such as the browser or netcat (nc)?
And do you realize that localhost means the android device itself? If the server is running on a development machine hosting an emulator, you need to use the special alias address for the development machine loopback instead. If your app is running on an actual phone, it's even more complicated to contact a server on your pc, unless you go via wifi or set up USB tethering.
I just change to my ethernet IP Address 192.168.1.80 instead of using "localhost" or "127.0.0.1", and it works ...
i got that solution. i changed the configuration file of Apache Server. (I have celled a web service that is in php)
精彩评论