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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:15:23+00:00 2026-06-11T15:15:23+00:00

I currently have multiple activity that needs to perform an asynctask for http post

  • 0

I currently have multiple activity that needs to perform an asynctask for http post and I wish to make the asynctask as another class file so that the different activity can call the asynctask to perform the http post request and onPostExecute, call the method httpResult(result) in the activity that called the asynctask. I have tried to pass the activity in but I am unable to call the method in onPostExecute. How can I do that?

public class MyActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_MyActivity);

    //logic here...
    AsyncHttpPost asyncHttpPost = new AsyncHttpPost("someContent", this, dialog);
    JSONObject data = new JSONObject();
    data.put("key", "value");
    try {
            asyncHttpPost.execute(data);
        }
        catch (Exception ex){
        ex.printStackTrace();
        }
    }
    public static void httpResult(String result) {
        //this method has to get called by asynctask to make changes to the UI
     }

}

AsyncHttpPost.java

public class AsyncHttpPost extends AsyncTask<JSONObject, String, String> {
String recvdjson;
String mData="";
private ProgressDialog dialog;
private Activity activity;


public AsyncHttpPost(String data, Activity activity, ProgressDialog dialog) {
    mData = data;
    this.activity = activity;
    this.dialog = dialog;
}

protected void onPreExecute()
{

    dialog.show();
}

    @Override
    protected String doInBackground(JSONObject... params) {

       //logic to do http request
           return "someStringResult";

    }

    protected void onPostExecute(String result) {
        dialog.dismiss();
        activity.httpResult(result); //This line gives me error : The method httpResult(String) is undefined for the type Activity


    }
}
  • 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-11T15:15:24+00:00Added an answer on June 11, 2026 at 3:15 pm

    Create an Interface called HttpResponseImpl in a seperate file and add the required method httpResult

    interface HttpResponseImpl{
        public void httpResult(String result);
    }
    

    Now implement this interface by you Activity class

    public class MyActivity extends Activity implements HttpResponseImpl{
    
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_MyActivity);
    
        //logic here...
        AsyncHttpPost asyncHttpPost = new AsyncHttpPost("someContent", this, dialog);
        JSONObject data = new JSONObject();
        data.put("key", "value");
        try {
                asyncHttpPost.execute(data);
            }
            catch (Exception ex){
            ex.printStackTrace();
            }
        }
    
        public void httpResult(String result){
            //this method has to get called by asynctask to make changes to the UI
        }
    }
    

    And your AsyncHttpPost class would be.

    public class AsyncHttpPost extends AsyncTask<JSONObject, String, String> {
    String recvdjson;
    String mData="";
    private ProgressDialog dialog;
    private HttpResponseImpl httpResponseImpl;
    
    
    public AsyncHttpPost(String data, HttpResponseImpl httpResponseImpl, ProgressDialog dialog) {
        mData = data;
        this.httpResponseImpl = httpResponseImpl;
        this.dialog = dialog;
    }
    
    protected void onPreExecute()
    {
    
        dialog.show();
    }
    
        @Override
        protected String doInBackground(JSONObject... params) {
    
           //logic to do http request
               return "someStringResult";
    
        }
    
        protected void onPostExecute(String result) {
            dialog.dismiss();
            httpResponseImpl.httpResult(result); 
    
    
        }
    }
    

    Implements this HttpResponseImpl interface with all you Activity class from which you want to do HttpRequest.

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

Sidebar

Related Questions

I currently have one project that currently contains multiple packages. These packages make up
I currently have an app that requires connection to a server in multiple activities.
We have a Flex app that is currently loading an XML file that contains
I am currently saving a message to a single file from multiple activity classes
I currently have a single web server hosting multiple WordPress sites which use W3TC.
I currently have an iPhone application with a tabbar and multiple viewcontrollers. All the
If you install multiple versions of python (I currently have the default 2.5, installed
I am currently trying to communicate a parent process which should have multiple children
I'm having an issue with multiple markers on google maps - I currently have
Currently I have a database with the following relationships: One Client has multiple Entities

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.