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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:29:23+00:00 2026-06-10T11:29:23+00:00

I have a simple class to handle http requests. Because this task needs to

  • 0

I have a simple class to handle http requests. Because this task needs to be performed in a different thread since Android 3.1 I have created the followin thread blocking class witch uses get to wait to the request to complete.

First time it works but after that I always get the time out or just freezes the application. Here is my code:

class MakeRequest extends AsyncTask<String,Integer,Void>
{

    @Override
    protected Void doInBackground(String... params) {
        DefaultHttpClient cli = new DefaultHttpClient();
        try {
            String url = params[0].replace(" ", "%20");
            HttpResponse resp = cli.execute(new HttpGet(url));
            BufferedReader read = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));
            String tmp = "",rezultat = "";
            setResult("");

            while ((tmp = read.readLine()) != null)
            {
                rezultat = rezultat + tmp;
            }
            setResult(rezultat);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            setResult("");
            return null;
        }
        return null;
    }



}

static public String req(Context ctx,String url,boolean want_resp)
{

    if (isOnline(ctx))
    {
        HttpRequester cc = new HttpRequester();
        MakeRequest test = cc.new MakeRequest();
        test.execute(url);
        if (want_resp)
        {
            try {
                test.get(30000, TimeUnit.MILLISECONDS);
            }
            catch (Exception e)
            {
                return "FAIL";
            }
            return result;
        }
        else return "OK";
    }
    else 
        {
        Toast.makeText(ctx, "Internet connection required", Toast.LENGTH_SHORT).show();
        setResult("");
        return "";
        }
}
  • 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-10T11:29:25+00:00Added an answer on June 10, 2026 at 11:29 am
    AsyncTask.get(long timeout, TimeUnit unit);
    

    Waits if necessary for at most the given time for the computation to complete, and then retrieves its result.So it blocks the UI.

    test.get(30000, TimeUnit.MILLISECONDS);
    

    This will make your main thread wait for the result of the AsyncTask at most 30000 milliseconds.

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

Sidebar

Related Questions

I have written a simple http server to handle POST requests: class MyHandler( BaseHTTPServer.BaseHTTPRequestHandler):
I used this tutorial http://delphi.about.com/od/kbthread/a/thread-gui.htm to create a class that asynchronously downloads a file
In have a controller class that handle a Contact object. In this controller I
I have simple class with width and height member fields which define number of
I have a simple class Student under namespace School. namespace XmlTestApp { public class
I have a simple class for use on JNI, which i need to export
I have a simple Class Hierarchy that I am trying to get to work
I have following simple class: @interface Article: NSObject { NSString *title; } @property (copy,
I have a simple class I'm serializing. [DataContract(Name = Test, Namespace = )] public
I have a simple class that contains some general information about the current web

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.