Android generic Asynctask

后端 未结 2 579
孤独总比滥情好
孤独总比滥情好 2021-01-16 02:17

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

相关标签:
2条回答
  • 2021-01-16 02:35

    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 讨论(0)
  • 2021-01-16 02:52

    Generic AsyncTask Example

    Call it like

    new RetrieveFeedTask(new OnTaskFinished()
            {
                @Override
                public void onFeedRetrieved(String feeds)
                {
                    //do whatever you want to do with the feeds
                }
            }).execute("http://enterurlhere.com");
    

    RetrieveFeedTask.class

    class RetrieveFeedTask extends AsyncTask<String, Void, String>
    {
        String HTML_response= "";
    
        OnTaskFinished onOurTaskFinished;
    
    
        public RetrieveFeedTask(OnTaskFinished onTaskFinished)
        {
            onOurTaskFinished = onTaskFinished;
        }
        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();
        }
    
        @Override
        protected String doInBackground(String... urls)
        {
            try
            {
                URL url = new URL(urls[0]); // enter your url here which to download
    
                URLConnection conn = url.openConnection();
    
                // open the stream and put it into BufferedReader
                BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    
                String inputLine;
    
                while ((inputLine = br.readLine()) != null)
                {
                    // System.out.println(inputLine);
                    HTML_response += inputLine;
                }
                br.close();
    
                System.out.println("Done");
    
            }
            catch (MalformedURLException e)
            {
                e.printStackTrace();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return HTML_response;
        }
    
        @Override
        protected void onPostExecute(String feed)
        {
            onOurTaskFinished.onFeedRetrieved(feed);
        }
    }
    

    OnTaskFinished.java

    public interface OnTaskFinished
    {
        public void onFeedRetrieved(String feeds);
    }
    
    0 讨论(0)
提交回复
热议问题