0

I have this code: class AttemptLogin extends AsyncTask {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(MyAkan.this);
        pDialog.setMessage("Attempting login...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected String doInBackground(String... arg0) {
        int success;
        String idnumber = etIdnumber.getText().toString();
        String password = etPassword.getText().toString();

        try {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("idnumber", idnumber));
            params.add(new BasicNameValuePair("password", password));

            JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST",
                    params);

            success = json.getInt(TAG_SUCCESS);
            if (success == 1) {
                Log.d("Login Successful!", json.toString());

                Intent i = new Intent("com.gpplsmje.mac.akan.AKANMENU");
                String id = json.getString(TAG_IDNUMBER);
                i.putExtra("idnumber", id);
                finish();
                startActivity(i);
                return json.getString(TAG_MESSAGE);
            } else if (success == 0) {
                Log.d("Login Failure!", json.getString(TAG_MESSAGE));
                return json.getString(TAG_MESSAGE);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String file_url) {
        // TODO Auto-generated method stub
        pDialog.dismiss();
        if (file_url != null) {
            Toast.makeText(MyAkan.this, file_url, Toast.LENGTH_LONG).show();
        }
    }

}

This app connects to a server and returns encoded JSON data. Whenever I test it on a local server on my machine, it works fine. It connects fine with the server. But when I turn off my server and use the app, the app loads but stops after a minute or so of login attempt. Is there a way to time the progress for maybe 20sec maximum, and then if it consumes the time it must dismiss the progressdialog and return to the main activity?

Thanks.

1 Answer 1

0

Your jsonParser object use HTTPURLConnection to do http requests ? If so there is a method setConnectTimeout.

If you are using DefaultHttpClient take a look a this question How to set HttpResponse timeout for Android in Java

Remember that above android 2.2 it's better to use HTTPURLConnection take a look at http://android-developers.blogspot.fr/2011/09/androids-http-clients.html for more details.

3
  • I'm not using HTTPURLConnection. I just used DefaultHttpClient and HttpPost for the connection.
    – TheGPWorx
    Jun 26, 2013 at 8:42
  • Take a look at this answer stackoverflow.com/questions/693997/…
    – JEY
    Jun 26, 2013 at 8:43
  • I've looked at the link and added it in my jsonParser code. it won't work. In the link it used HttpGet, will it work with HttpPost? that's what im using right now. Im still working on HTTPURLConnection. Thankz.
    – TheGPWorx
    Jun 26, 2013 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.