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 3404258
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:21:38+00:00 2026-05-18T05:21:38+00:00

I have a simple java code which gets html text from the input url:

  • 0

I have a simple java code which gets html text from the input url:

try {
    URL url =  new URL("www.abc.com");
    // Get the  response
    BufferedReader rd = new BufferedReader(new  InputStreamReader(url.openStream()));

      while ((line  = rd.readLine()) != null) {
      String code = code + line;

     } catch (IOException  e){}

I am using this code in an android project. Now the problem comes when there is no internet connectivity. The application just halts and later gives error.

Is there some way to break this after some fixed timeout, or even return some specific string after an exception is thrown. Can you please tell me how to do that??

  • 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-05-18T05:21:39+00:00Added an answer on May 18, 2026 at 5:21 am

    I don’t know what the default timeout is for URL, and a quick look at the javadocs doesn’t seem to reveal anything. So try using HttpURLConnection directly instead http://download.oracle.com/javase/1.5.0/docs/api/java/net/HttpURLConnection.html. This lets you set timeout values:

    public static void main(String[] args) throws Exception {
    
        URL url = new URL("http://www.google.com");
    
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(5000);    // 5 seconds
        conn.setRequestMethod("GET");       
        conn.connect();
        BufferedReader rd  = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    
        String line;
        while ((line = rd.readLine()) != null) {
            System.out.println(line);
        }
        conn.disconnect(); 
    }
    

    You can also set a read time out as well, as well as specify behaviour re redirects and a few other things.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to determine the jar file name from my java code. I
I am hoping someone can help me. I have a very simple question, but
This simple line of code gives the error The method getTextContent() is undefined for
I have a list of specific objects(meetings) which I need to group using a
Hello I have question about android spinner I have spinner which is populated by
I've created a simple xml file here: http://roberthan.host56.com/productsNew.xml which is quite simple, the root
I am just starting out learning Java, GAE etc. I have started a project
I have HttpSessionListener to listen for when sessions are created and destroyed. I have
I've been learning Android for about two weeks now (expert in as2/3). I have

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.