nawara
nawara

Reputation: 1167

Exception Connection refused while connecting Android device to PC

This i the code of Connection

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    DL_URL = "http://localhost:8080/project-server/rest/?value=test";
    downloadJsonFile();

}

 private void downloadJsonFile() {
        try {
            createFileAndDirectory();
            URL url = new URL(DL_URL);
            HttpURLConnection urlConnection;
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.setDoOutput(true);
            urlConnection.connect();


            int status = urlConnection.getResponseCode();

            Log.v("gg", Integer.toString(status));
            if (status == HttpURLConnection.HTTP_OK) {
            FileOutputStream fileOutput = new FileOutputStream(jsonFile);
            InputStream inputStream = urlConnection.getInputStream();
            byte[] buffer = new byte[1024];
            int bufferLength = 0;
            while ((bufferLength = inputStream.read(buffer)) > 0) {
            fileOutput.write(buffer, 0, bufferLength);
            }
            fileOutput.close();
            }else{
                 Toast.makeText(MainActivity.this,"error :)", Toast.LENGTH_LONG).show();

        }} catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        }

And this is my servles located @my own PC tha I try to connect to it (I use Windows 7)

   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doPost(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

 }

Erros I get

    06-18 12:42:55.940: W/System.err(4740): java.net.ConnectException: localhost/127.0.0.1:8080 - Connection refused
    06-18 12:42:55.950: W/System.err(4740):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:207)
    06-18 12:42:55.950: W/System.err(4740):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:437)
    06-18 12:42:55.950: W/System.err(4740):     at java.net.Socket.connect(Socket.java:983)
    06-18 12:42:55.950: W/System.err(4740):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:75)
    06-18 12:42:55.950: W/System.err(4740):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
    06-18 12:42:55.950: W/System.err(4740):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:322)
    06-18 12:42:55.950: W/System.err(4740):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
    06-18 12:42:55.950: W/System.err(4740):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:285)
    06-18 12:42:55.950: W/System.err(4740):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:267)
    06-18 12:42:55.950: W/System.err(4740):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:205)
    06-18 12:42:55.950: W/System.err(4740):     at com.example.test1.MainActivity.downloadJsonFile(MainActivity.java:42)
    06-18 12:42:55.950: W/System.err(4740):     at com.example.test1.MainActivity.onCreate(MainActivity.java:30)
    06-18 12:42:55.950: W/System.err(4740):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    06-18 12:42:55.950: W/System.err(4740):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
    06-18 12:42:55.950: W/System.err(4740):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
    06-18 12:42:55.950: W/System.err(4740):     at android.app.ActivityThread.access$1500(ActivityThread.java:123)
    06-18 12:42:55.950: W/System.err(4740):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
    06-18 12:42:55.950: W/System.err(4740):     at android.os.Handler.dispatchMessage(Handler.java:99)
    06-18 12:42:55.950: W/System.err(4740):     at android.os.Looper.loop(Looper.java:130)
    06-18 12:42:55.950: W/System.err(4740):     at android.app.ActivityThread.main(ActivityThread.java:3835)
    06-18 12:42:55.950: W/System.err(4740):     at java.lang.reflect.Method.invokeNative(Native Method)
    06-18 12:42:55.950: W/System.err(4740):     at java.lang.reflect.Method.invoke(Method.java:507)
    06-18 12:42:55.950: W/System.err(4740):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
    06-18 12:42:55.950: W/System.err(4740):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
    06-18 12:42:55.960: W/System.err(4740):     at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 7078

Answers (2)

LordPrimus
LordPrimus

Reputation: 31

Try to connect using "10.0.2.2:8080" instead of using "localhost:8080"

It'll work...

Upvotes: 3

user2302436
user2302436

Reputation: 518

You are trying to access http://localhost:8080/project-server/rest/?value=test, but localhost refers to the loopback address 127.0.0.1, so your request is sent to your Android device itself, not to your pc. Change localhost with the ip address of your pc.

Upvotes: 8

Related Questions