Where should I implement all my AsyncTasks?

I'm developing an android application which does different rest requests. I am not sure where I should implement all the AsynTasks. All in a separate files/classes? All in one static classes using inner classes?

1 answer

  • answered 2018-01-11 20:48 Habib Qureshi

    i am doing this for very long time, let me share my way with you:

    i have create a class which extends asyncTask

    public class GetJsonResponse extends AsyncTask<String, Void, String> {
            OnJsonResponse jsonResponse;
    
    
            public GetJsonResponse(OnJsonResponse jsonResponse) {
                this.jsonResponse = jsonResponse;
    
    
            }
    
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }
    
            @Override
            protected void onPostExecute(String response) {
                super.onPostExecute(response);
                this.jsonResponse.onJsonResponse(response);
            }
    
            @Override
            protected String doInBackground(String... params) {
                URL ulrn = null;
                try {
                    ulrn = new URL(params[0]);
                    M.l("doInBackground:",ulrn.toString());
                    HttpURLConnection con = (HttpURLConnection) ulrn.openConnection();
                    InputStream is = con.getInputStream();
                    BufferedReader in = new BufferedReader(
                            new InputStreamReader(is));
                    String inputLine;
                    StringBuffer response = new StringBuffer();
                    while ((inputLine = in.readLine()) != null) {
                        response.append(inputLine);
                    }
                    in.close();
                    con.disconnect();
                    return response.toString();
    
    
                } catch (Exception e) {
                    M.l(e.getMessage() + " exception");
                }
    
                return null;
            }
        }
    

    constructor receives interface which send response back from where this thread was created

        public GetJsonResponse(OnJsonResponse jsonResponse) {
            this.jsonResponse = jsonResponse;
    
    
        }
    

    doInBackground funtion connect to url and stream the input does't matter if it is image or json you can parse it after

       @Override
                protected String doInBackground(String... params) {
                    URL ulrn = null;
                    try {
                        ulrn = new URL(params[0]);
                        M.l("doInBackground:",ulrn.toString());
                        HttpURLConnection con = (HttpURLConnection) ulrn.openConnection();
                        InputStream is = con.getInputStream();
                        BufferedReader in = new BufferedReader(
                                new InputStreamReader(is));
                        String inputLine;
                        StringBuffer response = new StringBuffer();
                        while ((inputLine = in.readLine()) != null) {
                            response.append(inputLine);
                        }
                        in.close();
                        con.disconnect();
                        return response.toString();
    
    
                    } catch (Exception e) {
                        M.l(e.getMessage() + " exception");
                    }
    
                    return null;
                }
            }
    

    in post execute i just call the interface method

     @Override
        protected void onPostExecute(String response) {
            super.onPostExecute(response);
            this.jsonResponse.onJsonResponse(response);
        }
    

    here it is how i called the async function from any where in my application

    new GetJsonResponse(this).execute(url);
    

    you will receive your data in implemented method of you own interface

     @Override
        public void onJsonResponse(String response) {
       //do what ever you want to do with response 
    
    }