Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8289963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:39:21+00:00 2026-06-08T12:39:21+00:00

I am working on an Android app that needs to read a line from

  • 0

I am working on an Android app that needs to read a line from a web page right when it starts. I am doing this with the following code:

try{
        URL url = new URL("http://www.example.com");
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
        line = reader.readLine();
}
catch(Exception e){
        e.printStackTrace();
}

It is working fine, but sometimes the connection or the server are slow, and the app freezes or crashes.

I want to put a timeout of 5 seconds, and should it reach that timeout I want to show a toast to the user saying the network is busy, asking him to try again later.

I tried the HttpURLConnection setConnectTimeout() method but it didn’t work.

Any clues how I can achieve this? Thanks forehand.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T12:39:23+00:00Added an answer on June 8, 2026 at 12:39 pm

    Probably better solution if you setConnectionTimeout to 5 sec, catch SocketTimeoutException and show Toast from there. When you set ConnectionTimeout to some value and connection didn’t get response code will throw SocketTimeoutException. Here you can catch it and call handler to show a toast in UI. Finally will close the connection and release memory.

    class MyHttpClient extends DefaultHttpClient {
        @Override
        protected ClientConnectionManager createClientConnectionManager() {
            registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
            registry.register(new Scheme("https", 
                mSSLSocketFactory != null 
                ? mSSLSocketFactory 
                : SSLSocketFactory.getSocketFactory(), 
                443));
    
        return new SingleClientConnManager(getParams(), registry);
    }
    
       MyHttpClient httpClient = new MyHttpClient();
    
        // set http params
        HttpParams params = httpClient.getParams();
        params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, new Integer(30000));
        params.setParameter(CoreConnectionPNames.SO_TIMEOUT, new Integer(30000));
        httpClient.setParams(params);
    
        ....
        httpClient.execute(httpUriRequest) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing an app for android that needs to parse data from an XML
I'm working on an Android app that needs a lot of API calls. I've
I am currently working on an Android App that needs several overlapping SurfaceViews in
i am working on an Android app that needs showing a list[table], inside the
I am working on an Android app that utilizes the Google Maps API MapView,
I am working on an android app that has a database in which one
I have an Android app that I had working a few months ago which
I'm working on an App that runs a webservice hosted an Android device. I'm
I am working on a Corona sdk lua app for Android devices that does
I am working on an Android app that has multiple screens the user will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.