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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:39:31+00:00 2026-06-08T21:39:31+00:00

I have a standard AsyncTask like this: // starting AsyncTask in onCreate new TaskName().execute();

  • 0

I have a standard AsyncTask like this:

// starting AsyncTask in onCreate   
new TaskName().execute();


class TaskName extends AsyncTask<String, String, Void> {
    private ProgressDialog progressDialog = new ProgressDialog(Items.this);
    private InputStream is = null;
    private String result = "";

    protected void onPreExecute() {
        progressDialog.setMessage("Loading...");
        progressDialog.show();

        progressDialog.setOnCancelListener(new OnCancelListener() {

            public void onCancel(DialogInterface dialog) {
                TaskName.this.cancel(true);

            }
        });
    }

    @Override
    protected Void doInBackground(String... params) {
        String url_select = "my_link.php";

        param = new ArrayList<NameValuePair>();
        param.add(new BasicNameValuePair("Category", Category));

        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url_select);

        try {
            httpPost.setEntity(new UrlEncodedFormEntity(param));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();

            // read content
            is = httpEntity.getContent();

        } catch (Exception e) {

            Log.e("log_tag", "Error in http connection " + e.toString());
        }
        try {
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();
            String line = "";
            while ((line = br.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();

        } catch (Exception e) {
            // TODO: handle exception
            Log.e("log_tag", "Error converting result " + e.toString());
        }

        return null;

    }

    protected void onPostExecute(Void v) {

        try {

            JSONArray jArray = new JSONArray(result);
            JSONObject json_data = null;
            for (int i = 0; i < jArray.length(); i++) {
                json_data = jArray.getJSONObject(i);
                item = json_data.getString("item");

                items.add(item);

            }
        } catch (JSONException e1) {
            Toast.makeText(getBaseContext(), "No items!",
                    Toast.LENGTH_SHORT).show();
        } catch (ParseException e1) {
            e1.printStackTrace();
        }

        progressDialog.dismiss();

    }
}

However, I find myself having three near identical AsyncTasks doing this same thing. Accessing a PHP file, parsing JSON & printing JSON.

I search for a while to see if there is a standard class out there I can use but I could not find any. Is there a way to make this more efficient so I am not repeating so many times?

  • 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-08T21:39:34+00:00Added an answer on June 8, 2026 at 9:39 pm

    Maybe this will help, I am not sure if this is what you are asking.

    I am not sure there is anything like that, but you can certainly develop a pattern based mechanism. For example, you could create an interface called DoInAsyncTask that has a method doIt on it. Then have your AsyncTask class take one of these in its constructor.

    Then in the onExecute method, it just calls doIt on the instance that was passed in to it.

    Then, put all the specialized code for the parsing of each JSON string into a set of different classes that implement the DoInAsyncTask interface.

    Then you only have one AsyncTask class and all the specialized code goes into the separate parsing classes and you just pass in the correct one when you instantiate the one AsyncTask Class you have.

    Hard to write, hope this makes some sense and is what you were asking.

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

Sidebar

Related Questions

I have a standard form array in PHP like this. I need to account
I have standard multi-level menu like this one: <ul id=nav> <li><a href=#>Home</a></li> <li><a href=#>Dropdown</a>
I have a web application that have standard form authentication declared, like this: <login-config>
I have standard databindings setup for all my TextBoxes to an Object like this:
I have a standard list view setup with backbone that looks something like below.
I have a standard code like below to validate xml against xsd, but it
I have standard class library infrastructure assembly which i reference in my main application.
Does Java have standard functions for security like in php htmlspecialchars , strip_tags ?
Ok, So I have standard sencha-touch panel, and because this application needs to be
I have a standard char pointer which im trying to cast to a string.

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.