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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:26:32+00:00 2026-06-05T00:26:32+00:00

I am using OAuth2 on my android project. The idea is to use a

  • 0

I am using OAuth2 on my android project. The idea is to use a singleton HttpClient used with a ThreadSafeClientConnManager.

For a normal request to the server we construct an Authorization header and send that. The header is constructed from values received from the server. This works fine. However every 15 minutes we must get new values from the server to construct the header. To Received these values I must set the credentials like so.

client.getCredentialsProvider().setCredentials(
        new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
        new UsernamePasswordCredentials(creds.clientId, creds.clientSecret));

In order for this to work I must set up and new DefaultHttpClient. If I use the original singleton httpclient I receive some errors. My question is.. is it possible to set the credentials to be used only on this one request?

I noticed that there is an AuthScope. The host and port would not be suitable for this but maybe the realm would? I can’t find anything that tells me what a realm is or how to use it.

06-05 10:12:55.969: W/System.err(23843):
org.apache.http.NoHttpResponseException: The target server failed to
respond 06-05 10:12:55.969: W/System.err(23843): at
org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:85)
06-05 10:12:55.969: W/System.err(23843): at
org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174)
06-05 10:12:55.969: W/System.err(23843): at
org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:179)
06-05 10:12:55.969: W/System.err(23843): at
org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
06-05 10:12:55.969: W/System.err(23843): at
org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
06-05 10:12:55.975: W/System.err(23843): at
org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279)
06-05 10:12:55.975: W/System.err(23843): at
org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
06-05 10:12:55.975: W/System.err(23843): at
org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:504)
06-05 10:12:55.975: W/System.err(23843): at
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
06-05 10:12:55.975: W/System.err(23843): at
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
06-05 10:12:55.975: W/System.err(23843): at
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)

So After more testing I have found where the problem lies.

I want to configure a pooled connection manager like so

SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(
        new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
schemeRegistry.register(
        new Scheme("https", PlainSocketFactory.getSocketFactory(), 443));


ClientConnectionManager conManager = new ThreadSafeClientConnManager(new BasicHttpParams(), schemeRegistry);
    DefaultHttpClient httpClient = new DefaultHttpClient(conManager, new BasicHttpParams());

But when configure like this, I get the error above. If I use the normal default httpclient like so

 DefaultHttpClient httpClient = new DefaultHttpClient();

Then it works fine. Any ideas?

  • 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-05T00:26:33+00:00Added an answer on June 5, 2026 at 12:26 am

    As one requests needs basic auth credentials, and apparently calling setCredentials() causes an error, your best be might be to not use the credentials provider at all and set the Basic auth header manually (using setHeader() on the request) only when you need it.

    Edit: Here’s how you should create a threadsafe shared HttpClient instance:

     DefaultHttpClient createHttpclient(int timeout) {
            HttpParams params = new BasicHttpParams();
            HttpProtocolParams.setContentCharset(params,
                    HTTP.DEFAULT_CONTENT_CHARSET);
            HttpConnectionParams.setConnectionTimeout(params, timeout);
            HttpProtocolParams.setUserAgent(params, "MyApp/1.0");
            ConnPerRoute connPerRoute = new ConnPerRouteBean(10);
            ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute);
            ConnManagerParams.setMaxTotalConnections(params, 20);
    
            SchemeRegistry schemeRegistry = new SchemeRegistry();
            schemeRegistry.register(new Scheme("http", PlainSocketFactory
                    .getSocketFactory(), 80));
            final SSLSocketFactory sslSocketFactory = SSLSocketFactory
                    .getSocketFactory();
            schemeRegistry.register(new Scheme("https", sslSocketFactory, 443));
            ClientConnectionManager cm = new ThreadSafeClientConnManager(params,
                    schemeRegistry);
    
            return new DefaultHttpClient(cm, params);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to generate a request token using oauth2 in a Pyramid application for
I'm working on an Android app using the Windows Live services which uses OAuth2
Is it possible to obtain a OAuth2 token to use with Google APIs, using
I am successfully able to authenticate Facebook and Google accounts using my Oauth2 servlets.
I am using the OAuth 2.0 PHP Library to develop a OAuth 2.0 server
I am trying to write twitter app using android. The twitter login page opens
what is the correct grant_type for foursquare oauth2 access? I am using grant_type=authorization_code but
Anybody can please help in Android + Twitter Integration using OAuth. I already worked
I'm starting an Android app which will have multiple Activities. It will be using
I authenticating salesforce app using Oauth2. But I am not getting instance_url in access_token.

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.