0

In database we have login details {"email":"nagu@gmail.com","pwd":"12345"}.By using REST services I got the URL, I need to validate the credentials and move to next screen. But unable to move to next screen so please help me

public class LoginScreen extends DIBaseActivty {

public static final int CONNECTION_TIMEOUT=10000;
public static final int READ_TIMEOUT=15000;

private EditText editTextUsername;
private EditText editTextPassword;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login_screen);

    editTextUsername = (EditText)findViewById(R.id.emaiId);
    editTextPassword = (EditText)findViewById(R.id.password);

}

public void buttonLogin (View arg0)
{
    final String username = editTextUsername.getText().toString();
    final String password = editTextPassword.getText().toString();
    new TaskLogin().execute(username,password);
}

public class TaskLogin extends AsyncTask<String,String,String>
{
    ProgressDialog pdLoading = new ProgressDialog(LoginScreen.this);
    HttpURLConnection conn;
    URL url = null;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        pdLoading.setMessage("\t Loading...");
        pdLoading.setCancelable(false);
        pdLoading.show();
    }

    @Override
    protected String doInBackground(String... params) {
        try{
            url = new URL("http://localhost:8080/RestSpring/DialysisInfo/login");
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return "exception";
        }
        try{
            conn = (HttpURLConnection)url.openConnection();
            conn.setReadTimeout(READ_TIMEOUT);
            conn.setConnectTimeout(CONNECTION_TIMEOUT);
            conn.setRequestMethod("POST");

            conn.setDoInput(true);
            conn.setDoOutput(true);

            Uri.Builder builder = new Uri.Builder()
                    .appendQueryParameter("username",params[0])
                    .appendQueryParameter("password",params[1]);
            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 (IOException e1)
        {
            e1.printStackTrace();
            return "Exception";
        }
        try{
            int response_code = conn.getResponseCode();
            if(response_code==HttpURLConnection.HTTP_OK)
            {
                InputStream input = conn.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(input));
                StringBuilder result = new StringBuilder();
                String line;
                while ((line= reader.readLine())!=null)
                {
                    result.append(line);
                }
                return (result.toString());
            } else { return ("unsuccessful");
            }

        } catch (IOException e)
        {
            e.printStackTrace();
            return "Exception";
        } finally {
            conn.disconnect();
        }
    }

    @Override
    protected void onPostExecute(String result) {
        pdLoading.dismiss();
        if(result.equalsIgnoreCase("true"))
        {
            Intent intent = new Intent(LoginScreen.this,DIUserProfile.class);
            startActivity(intent);
            LoginScreen.this.finish();
        }else if (result.equalsIgnoreCase("false"))
        {
            Toast.makeText(LoginScreen.this, "Invalid email or password", Toast.LENGTH_LONG).show();
        } else if(result.equalsIgnoreCase("exception") || result.equalsIgnoreCase("unsuccessful"))
        {
            Toast.makeText(LoginScreen.this, "OOPs! Something went wrong. Connection Problem.", Toast.LENGTH_LONG).show();
        }
    }
}
}

Here is my error log

                      02-02 10:42:56.660 25016-27263/com.dialysis 

W/System.err: java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 8080) after 10000ms: isConnected failed: ECONNREFUSED (Connection refused)
    02-02 10:42:56.661 25016-27263/com.dialysis W/System.err:     at libcore.io.IoBridge.isConnected(IoBridge.java:238)
    02-02 10:42:56.661 25016-27263/com.dialysis W/System.err:     at libcore.io.IoBridge.connectErrno(IoBridge.java:171)
    02-02 10:42:56.661 25016-27263/com.dialysis W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:122)
    02-02 10:42:56.663 25016-27263/com.dialysis W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
    02-02 10:42:56.663 25016-27263/com.dialysis W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
    02-02 10:42:56.664 25016-27263/com.dialysis W/System.err:     at java.net.Socket.connect(Socket.java:882)
    02-02 10:42:56.664 25016-27263/com.dialysis W/System.err:     at com.android.okhttp.internal.Platform.connectSocket(Platform.java:174)
    02-02 10:42:56.664 25016-27263/com.dialysis W/System.err:     at com.android.okhttp.Connection.connect(Connection.java:152)
    02-02 10:42:56.665 25016-27263/com.dialysis W/System.err:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:276)
    02-02 10:42:56.665 25016-27263/com.dialysis W/System.err:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
    02-02 10:42:56.665 25016-27263/com.dialysis W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:382)
    02-02 10:42:56.665 25016-27263/com.dialysis W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)
    02-02 10:42:56.666 25016-27263/com.dialysis W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:217)
    02-02 10:42:56.666 25016-27263/com.dialysis W/System.err:     at com.dialysis.renalteam.app.ui.activities.LoginScreen$TaskLogin.doInBackground(LoginScreen.java:86)
    02-02 10:42:56.666 25016-27263/com.dialysis W/System.err:     at com.dialysis.renalteam.app.ui.activities.LoginScreen$TaskLogin.doInBackground(LoginScreen.java:49)
    02-02 10:42:56.666 25016-27263/com.dialysis W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:292)
    02-02 10:42:56.666 25016-27263/com.dialysis W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    02-02 10:42:56.666 25016-27263/com.dialysis W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
    02-02 10:42:56.666 25016-27263/com.dialysis W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    02-02 10:42:56.666 25016-27263/com.dialysis W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    02-02 10:42:56.666 25016-27263/com.dialysis W/System.err:     at java.lang.Thread.run(Thread.java:818)
    02-02 10:42:56.667 25016-27263/com.dialysis W/System.err: Caused by: android.system.ErrnoException: isConnected failed: ECONNREFUSED (Connection refused)
    02-02 10:42:56.667 25016-27263/com.dialysis W/System.err:     at libcore.io.IoBridge.isConnected(IoBridge.java:223)
    02-02 10:42:56.667 25016-27263/com.dialysis W/System.err:   ... 20 more
    02-02 10:42:56.714 25016-25071/com.dialysis W/EGL_emulation: eglSurfaceAttrib not implemented
    02-02 10:42:56.714 25016-25071/com.dialysis W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xb43cbb00, error=EGL_SUCCESS
    02-02 10:42:57.143 25016-25071/com.dialysis W/EGL_emulation: eglSurfaceAttrib not implemented
    02-02 10:42:57.144 25016-25071/com.dialysis W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xb43cbd20, error=EGL_SUCCESS
    02-02 10:52:41.787 25016-25030/com.dialysis W/art: Suspending all threads took: 5.515ms
Akshay
  • 1,161
  • 1
  • 12
  • 33

2 Answers2

1

From your logcat:

java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 8080) after 10000ms: isConnected failed: ECONNREFUSED (Connection refused)

Your Android app failed to connect to the server. Set debug breakpoints and check the following:

  1. Url is correct
  2. Method name is correct
  3. Parameters key and value are correct
  4. Server is working fine or not
David Rawson
  • 20,912
  • 7
  • 88
  • 124
0

Since you have not specified you are connected to a server from the device or emulator so I guess you are using your application in the emulator.

If you are referring your localhost on your system from the Android emulator then you have to use http://10.0.2.2:8080/ Because Android emulator runs in a Virtual Machine therefore here 127.0.0.1 or localhost will be emulator's own loopback address.

Replace: url = http://localhost:8080/RestSpring/DialysisInfo/login"

With : url = "http://10.0.2.2:8080/RestSpring/DialysisInfo/login"

Pranav Darji
  • 744
  • 3
  • 13