-1

I am trying to make a function called authenticate, which needs username and password as arguments. The function makes a new thread, posts data to the .php file and returns the result of authentication. The problem that I am facing is that an IOException occurs and I can't figure our why. I am sharing the relevant function and the LogCat here. Any help will be highly appreciated.

authenticate Function:

    private boolean authenticate (final String userName, final String password) throws ClientProtocolException, IOException, InterruptedException {

    Thread httpThread = new Thread (new Runnable () {

        @Override
        public void run() {
            HttpClient client = new DefaultHttpClient ();
            HttpPost postHandle = new HttpPost (projectFolder + "appSupport/appBackEnd.php");
            Log.d ("Connection",projectFolder + "appSupport/appBackEnd.php");

            List <NameValuePair> credentials  = new ArrayList <NameValuePair> ();

            credentials.add (new BasicNameValuePair ("action", "authenticate"));
            credentials.add (new BasicNameValuePair ("username", userName));
            credentials.add (new BasicNameValuePair ("password", password));

            try {

                postHandle.setEntity(new UrlEncodedFormEntity(credentials));
                Log.d ("Connection", "Entity Set!");

                HttpResponse response = client.execute(postHandle);
                Log.d ("Connection", "Executed!");

                String  res = response.getEntity().toString();
                Log.d ("Connection", "Response" + res);

                if (res.equals("1"))
                    result =  true;     // result is a global variable
                else
                    result = false;
            }
            catch (UnsupportedEncodingException e) {
                Log.d ("Connection", "Exception! UnsupportedEncodingException");
                 e.printStackTrace();
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                Log.d ("Connection", "Exception! ClientProtocolException");
                e.printStackTrace();
            } catch (IOException e) {
                Log.d ("Connection", "Exception! IOException");
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });

    httpThread.start();
    httpThread.join();
    return result;

}

LogCat:

01-12 23:17:33.566: D/PackageItemInfo(32301): loadIcon(cached)name=com.example.dummyproject.SignIn
01-12 23:17:33.636: D/dalvikvm(32301): GC_FOR_ALLOC freed 66K, 65% free 4474K/12760K, paused 23ms, total 23ms
01-12 23:17:33.666: I/dalvikvm-heap(32301): Grow heap (frag case) to 18.562MB for 12582928-byte allocation
01-12 23:17:33.696: D/dalvikvm(32301): GC_CONCURRENT freed 3K, 34% free 16759K/25052K, paused 2ms+2ms, total 31ms
01-12 23:17:34.327: D/libEGL(32301): loaded /system/lib/egl/libEGL_adreno200.so
01-12 23:17:34.327: D/libEGL(32301): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
01-12 23:17:34.337: D/libEGL(32301): loaded /system/lib/egl/libGLESv2_adreno200.so
01-12 23:17:34.347: I/Adreno200-EGL(32301): <qeglDrvAPI_eglInitialize:265>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB_2.5.04.02.02.040.400_msm8960_JB_2.5_CL3744273_release_AU (CL3744273)
01-12 23:17:34.347: I/Adreno200-EGL(32301): Build Date: 06/30/13 Sun
01-12 23:17:34.347: I/Adreno200-EGL(32301): Local Branch: 
01-12 23:17:34.347: I/Adreno200-EGL(32301): Remote Branch: quic/jb_2.5
01-12 23:17:34.347: I/Adreno200-EGL(32301): Local Patches: NONE
01-12 23:17:34.347: I/Adreno200-EGL(32301): Reconstruct Branch: AU_LINUX_ANDROID_JB_2.5.04.02.02.040.400 +  NOTHING
01-12 23:17:34.427: D/OpenGLRenderer(32301): Enabling debug mode 0
01-12 23:17:38.381: D/Connection(32301): http://192.168.1.2:8080/submitted/appSupport/appBackEnd.php
01-12 23:17:38.381: D/Connection(32301): Entity Set!
01-12 23:17:38.431: D/Connection(32301): Exception! IOException
01-12 23:17:38.431: W/System.err(32301): org.apache.http.conn.HttpHostConnectException: Connection to http://192.168.1.2:8080 refused
01-12 23:17:38.431: W/System.err(32301):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:191)
01-12 23:17:38.431: W/System.err(32301):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
01-12 23:17:38.431: W/System.err(32301):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
01-12 23:17:38.431: W/System.err(32301):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
01-12 23:17:38.431: W/System.err(32301):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
01-12 23:17:38.431: W/System.err(32301):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
01-12 23:17:38.431: W/System.err(32301):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
01-12 23:17:38.431: W/System.err(32301):    at com.example.dummyproject.SignIn$3.run(SignIn.java:107)
01-12 23:17:38.431: W/System.err(32301):    at java.lang.Thread.run(Thread.java:856)
01-12 23:17:38.442: W/System.err(32301): Caused by: java.net.ConnectException: failed to connect to /192.168.1.2 (port 8080): connect failed: ECONNREFUSED (Connection refused)
01-12 23:17:38.442: W/System.err(32301):    at libcore.io.IoBridge.connect(IoBridge.java:118)
01-12 23:17:38.442: W/System.err(32301):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
01-12 23:17:38.442: W/System.err(32301):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
01-12 23:17:38.442: W/System.err(32301):    at java.net.Socket.connect(Socket.java:847)
01-12 23:17:38.442: W/System.err(32301):    at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
01-12 23:17:38.442: W/System.err(32301):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:152)
01-12 23:17:38.442: W/System.err(32301):    ... 8 more
01-12 23:17:38.442: W/System.err(32301): Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
01-12 23:17:38.442: W/System.err(32301):    at libcore.io.Posix.connect(Native Method)
01-12 23:17:38.442: W/System.err(32301):    at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
01-12 23:17:38.442: W/System.err(32301):    at libcore.io.IoBridge.connectErrno(IoBridge.java:131)
01-12 23:17:38.442: W/System.err(32301):    at libcore.io.IoBridge.connect(IoBridge.java:116)
01-12 23:17:38.442: W/System.err(32301):    ... 13 more
01-12 23:17:38.442: D/DEBUG(32301): could not authenticate
A_Javed
  • 46
  • 8
  • Have you added the proper permissions to your manifest file, namely `INTERNET` for your `Socket` calls and `WRITE_EXTERNAL_STORAGE` for saving the data to a .php file – Willis Jan 12 '15 at 18:43
  • This is permission that I have added. isn't it enough? – A_Javed Jan 12 '15 at 18:55
  • Forget permssions, usual response of someone who hasn't read the stack trace. If you were missing a permission, the exception would tell you. You didn't say if you're using an emulator or not but read this http://developer.android.com/tools/devices/emulator.html#networkaddresses – Simon Jan 12 '15 at 19:04
  • @Simon I'm not using emulator, I am running directly on my cell phone i.e. Sony Xperia L. – A_Javed Jan 12 '15 at 19:17
  • P.S: I am using USBserver as my webserver – A_Javed Jan 12 '15 at 19:18

1 Answers1

0

I figured out the problem myself. The reason that the application was unable to connect to the server since I was using a different wifi connection on my PC (in which USBserver was running) and a different wifi connection on my mobile phone running my app.

A_Javed
  • 46
  • 8