3

I have a problem that I haven't solved yet and I need help.

When internet is slow application crashes. how can can I check connection timeout in asyntask.

I made an app that sometimes connect to web service to get data and i do that using async task

I want to make alert dialog on connection timeout when user can choose whether they want to retry or cancel, if they choose retry it will try to connect again

 public class login extends AsyncTask<Void,Void,Void> {

    InputStream ins;
    String status, result, s = null, data = "",js;
    int ss;
    int responseCode;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pdlg.setTitle("Checking");
        pdlg.setMessage("Please wait");
        pdlg.setCancelable(false);
        pdlg.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        StringBuilder sb = new StringBuilder();
        ArrayList al;
        try {
            URL url = new URL("http://....login.php");
            String param = "username=" + uname + "&password=" + pass;
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setConnectTimeout(15000);
            connection.setReadTimeout(15000);
            connection.setDoInput(true);
            connection.setDoOutput(true);

            OutputStream os = connection.getOutputStream();
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
            bw.write(param);
            bw.flush();
            bw.close();

            responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String line = "";
                while ((line = br.readLine()) != null) {
                    sb.append(line + "\n");
                }
            }
            data = sb.toString();
            JSONObject json = new JSONObject(data);

            status=json.getString("Status");//{"Login Status":"Success","Receipt Details":"No data available"}

           // js=json.getString("Login");//{"Login":"Failed"}



        } catch (MalformedURLException e) {
            Log.i("MalformedURLException", e.getMessage());
        } catch (IOException e) {
            Log.i("IOException", e.getMessage());
        } catch (JSONException e) {
            Log.i("JSONException", e.getMessage());
        }

        return null;
    }

    protected void onPostExecute(Void result) {
        super.onPostExecute(result);

        String status1=status.trim();


      if (status1.equals("Success")) {
    Toast.makeText(getApplicationContext(), "Login Succes  !!", Toast.LENGTH_SHORT).show();

          Intent i = new Intent(Login.this, Home.class);
          startActivity(i);
            finish();
          SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
          SharedPreferences.Editor editor = sharedPreferences.edit();
          editor.putBoolean("first_time", false);
          editor.putString("userrname", uname);
          editor.putString("password",pass);
          editor.apply();
          Toast.makeText(getApplicationContext(),"welcome : "+uname,Toast.LENGTH_LONG).show();

      }

else {
    Toast t=Toast.makeText(Login.this, "Username or Password is Incorrect", Toast.LENGTH_LONG);
          t.setGravity(Gravity.BOTTOM,0,0);
                        t.show();
}

        pdlg.dismiss();


    }



   }
Nitish Kumar Diwakar
  • 663
  • 4
  • 14
  • 25

5 Answers5

1

use this two catch blocks to handle ConnectionTimeOut and socketTimeOut Exceptions

        catch (SocketTimeoutException bug) {
            Toast.makeText(getApplicationContext(), "Socket Timeout", Toast.LENGTH_LONG).show();
        } 
        catch (ConnectTimeoutException bug) {
            Toast.makeText(getApplicationContext(), "Connection Timeout", Toast.LENGTH_LONG).show();
        } 
Mahesh Gawhane
  • 348
  • 3
  • 20
1

For Connection time out add SocketTimeoutException in your catch block and its crashing because without null checking, you are trying to trim the string in onPostExecute

You should do like this, and check before using status

if(TextUtil.isEmpty(status)) {
   pdlg.dismiss();
   // We are getting empty response
   return;
}
String status1=status.trim();
Muthukrishnan Rajendran
  • 11,122
  • 3
  • 31
  • 41
1

What you are trying to do is previously done in some great networking lib. So I urge you to use one of the widley used network lib. Volley.

Or if you want to understand you can just check the response status(or status code should be 408. I guess for connection time out) if it will return "connection time out" then you can call your code to the http client again to perform your task, you can also add a retry count to try for 2-3 times and then give up and send response to onpostexecute method.

Hope this helps.

Mahesh Gawhane
  • 348
  • 3
  • 20
MobileEvangelist
  • 2,583
  • 1
  • 25
  • 36
1

You can catch the Connection timeout exception in your code and then set the status according to your requirement and check for that status in onPostExecute to show the alert Dialog

try {
            URL url = new URL("http://....login.php");
            String param = "username=" + uname + "&password=" + pass;
    // Your URL connection code here
catch (ConnectTimeoutException e) {
        Log.e(TAG, "Timeout", e);
        status="timeout"
    } catch (SocketTimeoutException e) {
        Log.e(TAG, " Socket timeout", e);
        status="timeout"
    }

in onPostExecute

if (status.equals("timeout")) {
    // Show Alert Dialog.
}
Kapil G
  • 4,081
  • 2
  • 20
  • 32
1

You can use getErrorStream() ,

HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream inp;

// if some error in connection 
 inp = connection.getErrorStream();

check this answer for more details..

according to the doc it returns

an error stream if any, null if there have been no errors, the connection is not connected or the server sent no useful data

.

Omar Dhanish
  • 885
  • 7
  • 18