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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:22:06+00:00 2026-05-21T02:22:06+00:00

I have an application which is a RESTful API consumer, and I wish to

  • 0

I have an application which is a RESTful API consumer, and I wish to have a timeout on my connection with the API.

As I’ve searched, and also tested, the HttpURLConnection.setReadTimeout() method doesn’t work, so the solution I found was to use an AsyncTask which will try to connect to the server and then pass the timeout to the AsyncTask.get().

It works, partially. The problem is when I do the following:

  • Enter the application with the WiFi turned on. I click “Login” button and get “Invalid/User password”. Ok.
  • Turn off the WiFi, click “Login” button. The application tries to connect but after 5 seconds (the timeout I chose) it shows me the notification dialog saying I’m not connected. Ok, everything as expected.
  • Turn on back the WiFi, click “Login” button. It stills working as if I was not connected, always showing the dialog. I can wait many seconds but the behaviour is the same as If I was disconnected.

I debugged all my code, step by step using Eclipse and there’s nothing wrong with the logic.
My HttpURLConnection is always a new object, so I’m not trying to use the same connection object to connect after the WiFi is turned on back…
Also I’m using the Scribe library, for OAuth, but I checked the source code and everything seems ok, except that I changed the method which creates a connection to always use a new instance.

I’m starting to think that Android is “caching” my connection object or the AsyncTask object…

Below some code of my RequestManager class:

public class RequestManager {
    private static RequestManager self = new RequestManager(); 
    private Request currentRequest;

    private static final long TIMEOUT = 5000;

    public static RequestManager getInstance() {
        return self;
    }

    public void startRequest(Request request) {
        if (currentRequest != null) return;
        currentRequest = request;
    }

    public String getResponse() throws ConnectionErrorException {
        RequestThreat rt = new RequestThread();
        rt.execute(currentRequest);  
        try {
            return (String) rt.get(TIMEOUT, TimeUnit.MILISECONDS);
        } catch (InterruptedException e) {
        } catch (ExecutionException e) {
        } catch (TimeoutException e) {
            throw new ConnectionErrorException();
        } finally {
            endRequest();
        }
        return null;
    }

    public void endRequest() {
        currentRequest = null;
    }

    private class RequestThread extends AsyncTask<Request, Integer, String> {
        @Override
        protected String doInBackground(Request... requestParams) {
            return requestParams[0].send().getBody();
        }
    }

}

Also in the method I call getResponse() I’m calling endRequest() after.

Any thoughts?

  • 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-21T02:22:06+00:00Added an answer on May 21, 2026 at 2:22 am

    You should check to see if a network connection is available before trying to access the network. Have a look at this quesion:

    • How to respect network use settings

    Specifically:

    NetworkInfo networkInfo = cm.getActiveNetworkInfo();
    if (networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected()) {
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to develop some RESTful web services which have to access some
I have application which is working best with Wifi,3G.. Now what i want is
I am using tomcat as my webserver. i have application which needs a tomcat
I have an application which I am creating for Ubuntu. I come from a
In my ruby application I have created a backend for managing quizzes. It consists
I am planning a Web Application and I am currently evaluating Frameworks. I have
I have a pretty common case for nested routes, I feel like, that looks
In our web application, I am allowing users to edit their profiles, using a
How would one go about sending data back to server, from an android application?
How to I represent a complex resource for a REST post? Hello, Currently I

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.