srg
srg

Reputation: 31

Address family not supported by protocol exception

I'm trying to send a couple of values from an android application to a web service which I've setup. I'm using Http Post to send them but when I run the application I get the error- request time failed java.net.SocketException: Address family not supported by protocol.

I get this while debugging with both the emulator as well as a device connected by wifi. I've already added the internet permission using:

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

This is the code i'm using to send the values

    void insertData(String name, String number) throws Exception {
    String url = "http://192.168.0.12:8000/testapp/default/call/run/insertdbdata/";
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);

    try {
        List<NameValuePair> params = new ArrayList<NameValuePair>(2);

        params.add(new BasicNameValuePair("a", name));

        params.add(new BasicNameValuePair("b", number));
        post.setEntity(new UrlEncodedFormEntity(params));
        HttpResponse response = client.execute(post);
    }catch(Exception e){
        e.printStackTrace();
    }

Also I know that my web service work fine because when I send the values from an html page it works fine -

<form name="form1" action="http://192.168.0.12:8000/testapp/default/call/run/insertdbdata/" method="post">
    <input type="text" name="a"/>
    <input type="text" name="b"/>
    <input type="submit"/>

I've seen questions of similar problems but haven't really found a solution.

Thanks

Upvotes: 3

Views: 8768

Answers (5)

Sagar Maiyad
Sagar Maiyad

Reputation: 12733

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

Add this permission in AndroidManifest.xml file.

Upvotes: 0

Jeshurun
Jeshurun

Reputation: 23186

Make sure you don't have a firewall rule on your server, allowing only certain ip addresses to connect. On the server I was using I had ufw running that only allowed my computer's IP address on the desired port.

Upvotes: 0

Parth Doshi
Parth Doshi

Reputation: 4208

Try adding this to your AndroidManifest.xml as well:

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

You also need to catch this Exception:

 catch(SocketException ex)
       {
         Log.e("Error : " , "Error on soapPrimitiveData() " + ex.getMessage());
           ex.printStackTrace();
       }

Upvotes: 1

mugume david
mugume david

Reputation: 635

substitute your ip 192.168.0.12:8000 with 10.0.2.2:8000. Tht should working for debugging

Upvotes: 0

providence
providence

Reputation: 29463

I have had this issue before. It was nothing to do with my code, but rather that the connection was not being established. Check to make sure you're using the right ip/port. If you can, go on the serving machine and check if the connection is being established on that side.

Upvotes: 0

Related Questions