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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:58:19+00:00 2026-05-28T01:58:19+00:00

I want to implement a class which will handle all HTTP Requests of my

  • 0

I want to implement a class which will handle all HTTP Requests of my application, which will be basically:

  • Get a list of business (GET);
  • Execute a login (POST);
  • Update the location (POST).

So, I will have to get the result string from the server (JSON) and pass it to another methods to handle the responses.

I currently have this methods:

public class Get extends AsyncTask<Void, Void, String> {
    @Override
    protected String doInBackground(Void... arg) {
        String linha = "";
        String retorno = "";

        mDialog = ProgressDialog.show(mContext, "Aguarde", "Carregando...", true);

        // Cria o cliente de conexão
        HttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet(mUrl);

        try {
            // Faz a solicitação HTTP
            HttpResponse response = client.execute(get);

            // Pega o status da solicitação
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();

            if (statusCode == 200) { // Ok
                // Pega o retorno
                BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

                // Lê o buffer e coloca na variável
                while ((linha = rd.readLine()) != null) {
                    retorno += linha;
                }
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return retorno;
    }

    @Override
    protected void onPostExecute(String result) {
        mDialog.dismiss();
    }
}

    public JSONObject getJSON(String url) throws InterruptedException, ExecutionException {
        // Determina a URL
        setUrl(url);

        // Executa o GET
        Get g = new Get();

        // Retorna o jSON
        return createJSONObj(g.get());
    }

But the g.get() returns a empty response. How can I fix 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-28T01:58:20+00:00Added an answer on May 28, 2026 at 1:58 am

    I think you didn’t understand exactly the way AsyncTask works. But I believe you wish to reuse the code for different tasks; if so, you can create an abstract class and then extend it implementing an abstract method you created. It should be done like this:

    public abstract class JSONTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... arg) {
            String linha = "";
            String retorno = "";
            String url = arg[0]; // Added this line
    
            mDialog = ProgressDialog.show(mContext, "Aguarde", "Carregando...", true);
    
            // Cria o cliente de conexão
            HttpClient client = new DefaultHttpClient();
            HttpGet get = new HttpGet(mUrl);
    
            try {
                // Faz a solicitação HTTP
                HttpResponse response = client.execute(get);
    
                // Pega o status da solicitação
                StatusLine statusLine = response.getStatusLine();
                int statusCode = statusLine.getStatusCode();
    
                if (statusCode == 200) { // Ok
                    // Pega o retorno
                    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    
                    // Lê o buffer e coloca na variável
                    while ((linha = rd.readLine()) != null) {
                        retorno += linha;
                    }
                }
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            return retorno; // This value will be returned to your onPostExecute(result) method
        }
    
        @Override
        protected void onPostExecute(String result) {
            // Create here your JSONObject...
            JSONObject json = createJSONObj(result);
            customMethod(json); // And then use the json object inside this method
            mDialog.dismiss();
        }
    
        // You'll have to override this method on your other tasks that extend from this one and use your JSONObject as needed
        public abstract customMethod(JSONObject json);
    }
    

    And then the code on your activity should be something like this:

    YourClassExtendingJSONTask task = new YourClassExtendingJSONTask();
    task.execute(url);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to implement a class which can be used by two classes of
I want to implement a generic method on a generic class which would allow
I want to implement a class which its fields could change (add new fields)
I want to implement a scheduler class, which any object can use to schedule
I want to have a class which implements an interface, which specifies the specific
I want to implement a derived class that should also implement an interface, that
given this class definition: public class Frame { IFrameStream CapturedFrom; } I want implement
I want to implement in Java a class for handling graph data structures. I
I want to implement a function in the base class but I also want
I want to implement a custom trace listener like follows: public class TraceListener :

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.