0

I am getting the following exception when i run my android app: I am getting inside the asynctask but logging this exception.

03-15 16:49:21.456  16854-16892/com.ashwinbhy.phpwrite W/Ashwin﹕ java.net.SocketException: socket failed: EACCES (Permission denied)

I have included this permissions inside my manifest.xml:

<uses-permission android:name="android.permission.internet"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.access_network_state"/>

The code is:

 try {

                URL url = new URL("http://10.0.0.2/tt.php");
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setReadTimeout(10000);
                conn.setConnectTimeout(15000);
                conn.setRequestMethod("POST");
                conn.setDoInput(true);
                conn.setDoOutput(true);

                Uri.Builder builder = new Uri.Builder()
                        .appendQueryParameter("bus_id", busid)
                        .appendQueryParameter("lat", lat)
                        .appendQueryParameter("lon", lon);

                String query = builder.build().getEncodedQuery();

                OutputStream os = conn.getOutputStream();
                BufferedWriter writer = new BufferedWriter(
                        new OutputStreamWriter(os, "UTF-8"));
                writer.write(query);
                writer.flush();
                writer.close();
                os.close();

                conn.connect();
            }
            catch(Exception e){
                Log.w("Ashwin",e.toString()+"inside the exception log");
                return "error";
            }

Can someone please point where am i going wrong ?

I am addding the log:

03-15 17:15:42.140    9003-9034/com.ashwinbhy.phpwrite I/System.out﹕ open:http://10.0.0.2/tt.php
03-15 17:15:42.141    9003-9034/com.ashwinbhy.phpwrite D/libc-netbsd﹕ [getaddrinfo]: hostname=10.0.0.2; servname=(null); cache_mode=(null), netid=0; mark=0
03-15 17:15:42.141    9003-9034/com.ashwinbhy.phpwrite D/libc-netbsd﹕ [getaddrinfo]: ai_addrlen=0; ai_canonname=(null); ai_flags=4; ai_family=0
03-15 17:15:42.142    9003-9034/com.ashwinbhy.phpwrite I/System.out﹕ [CDS][DNS] getAllByNameImpl netId = 0
03-15 17:15:42.142    9003-9034/com.ashwinbhy.phpwrite D/libc-netbsd﹕ [getaddrinfo]: hostname=10.0.0.2; servname=(null); cache_mode=(null), netid=0; mark=0
03-15 17:15:42.142    9003-9034/com.ashwinbhy.phpwrite D/libc-netbsd﹕ [getaddrinfo]: ai_addrlen=0; ai_canonname=(null); ai_flags=4; ai_family=0
03-15 17:15:42.143    9003-9034/com.ashwinbhy.phpwrite I/System.out﹕ [CDS]rx timeout:10000
03-15 17:15:42.143    9003-9034/com.ashwinbhy.phpwrite I/System.out﹕ [socket][3] connection /10.0.0.2:80;LocalPort=55951(15000)
03-15 17:15:42.143    9003-9034/com.ashwinbhy.phpwrite I/System.out﹕ [CDS]connect[/10.0.0.2:80] tm:15

1 Answer 1

1

Try putting proper case for permissions. They are case sensitive.

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

Update

Your IP is also wrong. It should be, 10.0.2.2. So your URL will become,

http://10.0.2.2/tt.php

It should work fine on the emulator.

Sign up to request clarification or add additional context in comments.

8 Comments

Thank you. I am not getting exception but my page is not accessible still. I am not getting any error.
i am running it on local network 10.0.0.2/tt.php works fine from my browser
i am conncted to same wifi it works fine from my mobile browser but not working from app. the post request is not going.
For mobile you need to give the IP starting with something like 192.168.xxx.xxx.
Can you please see the log i have added in the comments.
|

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.