0

i need to show up alert message for Http web call Time out exception.(i.e)I need to show alert like "Server connection timed out" for certain period(like 5 sec) of web call in android.How could i do that?

My code:

public class MainActivity extends Activity {

static InputStream is = null;
static JSONObject jObj = null;
static HttpClient httpClient;
String json, Token;
private ProgressDialog progressDialog;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    progressDialog = ProgressDialog.show(MainActivity .this, "", "Loading...",
            true);
    new Thread() {
        // Run Mehod
        @Override
        public void run() {
            getCC();
            messagehandler.sendEmptyMessage(0);
        } // End of run
    }.start();
}

private Handler messagehandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        if (msg.what == 0) {
            progressDialog.dismiss();
            String response=json;
            //Do steps from response
        }

    }

};

private void getCC() {
    SSLCertificate();
    // HttpClient client = new DefaultHttpClient();
    String getURL = "My URL";
    HttpGet get = new HttpGet(getURL);

    get.setHeader("Accept", "*/*,text/xml,application/json");
    get.setHeader("Cache-Control", "no-cache");
    get.setHeader("Content-type", "application/json");

    HttpResponse response;
    try {
        // defaultHttpClient

        response = httpClient.execute(get);
        HttpEntity httpEntity = response.getEntity();
        is = httpEntity.getContent();

    }catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {

    }

}

private static void SSLCertificate() {
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setContentCharset(params, "HTTP.ISO-8859-1");
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    schemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(),
            443));

    ClientConnectionManager ccm = new ThreadSafeClientConnManager(params,
            schemeRegistry);
    HttpConnectionParams.setConnectionTimeout(params, 300);

    HttpConnectionParams.setSoTimeout(params, 300);

    httpClient = new DefaultHttpClient(params);

    httpClient = new DefaultHttpClient(ccm, params);

}

} Thanks.

sanjay
  • 2,590
  • 17
  • 55
  • 88

2 Answers2

0
final HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 10000);

HttpClient client = new DefaultHttpClient(httpParams);
HttpGet request = new HttpGet(url);

Will throw an Exception if it takes longer than 10000 millis

Ken Wolf
  • 23,133
  • 6
  • 63
  • 84
  • Where i need to put alert? – sanjay Jun 06 '13 at 08:57
  • Amend your code to include the params above, surround with `try/catch`, put the alert code in the `catch` block. Not 100% sure what exception it throws. I think it's either `IOException` or `ConnectTimeoutException` or `SocketTimeoutException` - google it :) – Ken Wolf Jun 06 '13 at 08:59
0

Use HttpParams:

int timeout = (int) (5 * DateUtils.SECOND_IN_MILLIS);
params.setConnectionTimeout(params, timeout);
params.setSoTimeout(params, timeout);

Then, surround with try catch.

dragostis
  • 2,574
  • 2
  • 20
  • 39