0

I've been trying to set up a client/server communication. My servlet works fine in the browser and when called from a simple java project. However, when I use the same code in an Android project it doesn't return anything.

Here is my code:

package com.jad.carpooling;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.AsyncTask;
import android.util.Log;

public class HttpRequest {
    public void getTrial() {
        new Async().execute();          
    }


    public class Async extends AsyncTask <String, Integer, String>{

        @Override
        protected String doInBackground(String... params) {
            Log.i(null, "entered");
            // TODO Auto-generated method stub
            URL url;
            BufferedReader reader = null;
            String s = "";
            try {
                url = new URL("http://localhost:8080/CarpoolServer/DemoServlet");
                URLConnection con = url.openConnection();
                reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
                String line = reader.readLine().toString();
                while ((line = reader.readLine()) != null) {
                    s = s + line;
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }  catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            Log.i(null, "response: " + s);
            return s;
        }

    }

}

There are no errors in the logcat, just a bunch of warnings. Here is the logcat:

05-05 08:18:17.199: W/System.err(816):  at libcore.io.IoBridge.socket(IoBridge.java:583)
05-05 08:18:17.389: W/System.err(816):  at java.net.PlainSocketImpl.create(PlainSocketImpl.java:201)
05-05 08:18:17.470: W/System.err(816):  at java.net.Socket.checkOpenAndCreate(Socket.java:663)
05-05 08:18:17.489: W/System.err(816):  at java.net.Socket.connect(Socket.java:807)
05-05 08:18:17.529: W/Trace(816): Unexpected value from nativeGetEnabledTags: 0
05-05 08:18:17.549: W/System.err(816):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:76)
05-05 08:18:17.569: W/System.err(816):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
05-05 08:18:17.579: W/Trace(816): Unexpected value from nativeGetEnabledTags: 0
05-05 08:18:17.609: W/Trace(816): Unexpected value from nativeGetEnabledTags: 0
05-05 08:18:17.609: W/Trace(816): Unexpected value from nativeGetEnabledTags: 0
05-05 08:18:17.609: W/Trace(816): Unexpected value from nativeGetEnabledTags: 0
05-05 08:18:17.639: W/Trace(816): Unexpected value from nativeGetEnabledTags: 0
05-05 08:18:17.659: W/System.err(816):  at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
05-05 08:18:17.759: W/System.err(816):  at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
05-05 08:18:17.829: W/System.err(816):  at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
05-05 08:18:17.829: W/System.err(816):  at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
05-05 08:18:17.829: W/System.err(816):  at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
05-05 08:18:17.829: W/System.err(816):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
05-05 08:18:17.829: W/System.err(816):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
05-05 08:18:17.829: W/System.err(816):  at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)
05-05 08:18:17.949: W/System.err(816):  at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
05-05 08:18:17.979: W/System.err(816):  at com.jad.carpooling.HttpRequest$Async.doInBackground(HttpRequest.java:73)
05-05 08:18:17.979: W/System.err(816):  at com.jad.carpooling.HttpRequest$Async.doInBackground(HttpRequest.java:1)
05-05 08:18:17.979: W/System.err(816):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-05 08:18:18.069: W/System.err(816):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-05 08:18:18.089: W/Trace(816): Unexpected value from nativeGetEnabledTags: 0
05-05 08:18:18.089: W/Trace(816): Unexpected value from nativeGetEnabledTags: 0
05-05 08:18:18.129: W/System.err(816):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-05 08:18:18.129: W/System.err(816):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-05 08:18:18.139: W/System.err(816):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-05 08:18:18.139: W/System.err(816):  at java.lang.Thread.run(Thread.java:856)
05-05 08:18:18.139: W/System.err(816): Caused by: libcore.io.ErrnoException: socket failed: EACCES (Permission denied)
05-05 08:18:18.239: W/System.err(816):  at libcore.io.Posix.socket(Native Method)
05-05 08:18:18.239: W/System.err(816):  at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:181)
05-05 08:18:18.239: W/System.err(816):  at libcore.io.IoBridge.socket(IoBridge.java:568)

The final Log.i is logged but the String 's' is empty. Any ideas?

Jad
  • 115
  • 2
  • 5
  • 10

1 Answers1

1

From your logs it appears it is missing INTERNET permission add this to your AndroidManifest.xml

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

Similar questions on SO:

  1. Error message 'java.net.SocketException: socket failed: EACCES (Permission denied)'
  2. Why I am getting the HttpHostConnectException

Update:

Localhost for an Android application means Mobile/Emulator etc, change it to the IP of the PC where you are connecting to like below:

url = new URL("http://MY_IP:8080/CarpoolServer/DemoServlet");
Community
  • 1
  • 1
mprabhat
  • 20,107
  • 7
  • 46
  • 63
  • what is says same exception ? where did you place your permission ? It should be after sdkversion and before application. – mprabhat May 05 '13 at 08:41
  • I think this might help: 05-05 08:45:12.796: W/System.err(1030): java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 8080): connect failed: ECONNREFUSED (Connection refused) – Jad May 05 '13 at 08:47
  • it is up. i tried it on my browser right before trying it on my android application – Jad May 05 '13 at 08:50
  • this removed all the warning but two: 05-05 09:12:14.156: W/Trace(1291): Unexpected value from nativeGetEnabledTags: 0 05-05 09:12:14.216: W/IInputConnectionWrapper(1291): showStatusIcon on inactive InputConnection But the response is still nothing – Jad May 05 '13 at 09:16
  • @Jad glad, one reason could be because you are ignoring first line which you have read **String line = reader.readLine().toString();** this is being ignored. Instead you can do something like this **String line = ""; while((line = reader.readLine()) != null) { }** – mprabhat May 05 '13 at 09:42