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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:43:16+00:00 2026-05-31T09:43:16+00:00

I am trying to implement ping using HttpGet but behavior is random. I am

  • 0

I am trying to implement ping using HttpGet but behavior is random.

I am having following code which test the internet/server connectivity:

boolean result = false;
HttpGet request = new HttpGet("www.MyServer.com");
HttpParams httpParameters = new BasicHttpParams();
HttpClient httpClient = new DefaultHttpClient(httpParameters);

try
{
    HttpConnectionParams.setConnectionTimeout(httpParameters, 6000);
    HttpConnectionParams.setSoTimeout(httpParameters, 6000); 
    HttpResponse response = httpClient.execute(request);

    int status = response.getStatusLine().getStatusCode();

    if (status == HttpStatus.SC_OK) 
    {
        result = true;
    }

}
catch(Exception e)
{
    e.printStackTrace();
    result = false;
}

Log.d("App", "Ping Result:"+result);

Above code I am running in thread as it may take time.
When I run this test for first time then I get result as true, but then after behavior is random, some times it given me error ‘host unreachable’ and I get result as false.

I just want to test is server is reachable from the currently configured Android network.

Is there any reliable API to test the internet/server connectivity?

UPDATE:

In a Service i have following function which initiates the test.

void startTest()
{ 
    ServerTestThread mServerTestThread = new ServerTestThread()
    mServerTestThread.start();
}

class ServerTestThread extends Thread 
{
    boolean result = false;
    public void run() 
    {   
         //HttpGet code

                 //Send Message TO GUI Thread with result.
    }
}

Above startTest function is creating instance of the test thread and calling start function. when test is done I am sending message to main thread which contains the result.

Thanks.

  • 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-31T09:43:17+00:00Added an answer on May 31, 2026 at 9:43 am

    There is no problem with your code. That means either:

    1. Sometimes server is really unreachable
    2. Connection is slow and it time outs before server is reached.

    So test setting timeout to some larger value, such as 60000(60sec), and check again. If it works, then you know it was because of timeout.

    EDIT

    Also please make this change, maybe it gives us more info:

    Log.d("App", "Status:" + status);
    if (status == HttpStatus.SC_OK) 
    {
        result = true;
    }
    

    EDIT2

    class ServerTestThread extends Thread 
    {
    
        public static boolean result = false;
        public static HttpGet request = new HttpGet("www.MyServer.com");
        public static HttpParams httpParameters = new BasicHttpParams();
        public static HttpClient httpClient = new DefaultHttpClient(httpParameters);
    
        boolean result = false;
        public void run() 
        {   
             //HttpGet code
    
                     //Send Message TO GUI Thread with result.
        }
    
    }
    

    As a bonus,
    this will tell if you if you’re connected to a network.

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

Sidebar

Related Questions

Currently trying to implement a simple ping program to teach myself about network programming
I am trying to implement LRU Cache using C++ . I would like to
I'm trying to find a really good example for implementing the MetaWeblog API using
I'm trying to create a generic graphics export tool which works by implementing the
All I am currently trying implement something along the lines of dim l_stuff as
I'm using Clojure, but I can read Java, so this isn't a Clojure specific
I am trying to implement a SOAP client to handle electronic reporting of California
I am trying to write some SQL that pulls from several tables using unions,
I'm implementing a WebScript using Alfresco's JavaScript. l'm trying to insert string to a
I'm implementing a custom text box using -interpretKeyEvents: and am trying to figure out

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.