0

i wrote those threads:
How to manage multiple Async Tasks efficiently in Android
Running multiple AsyncTasks at the same time -- not possible?

but didnt find answer for my question, maybe someone can help..

I have android app which makes Login POST and getting json response,
if the Json is OK i need to POST another data to get another response.

i have extends Async Class which doing the post to the URL:

public class AsyncHttpPost extends AsyncTask<String, String, String> {
    private HashMap<String, String> mData = null;

    public AsyncHttpPost(HashMap<String, String> data) {
        mData = data;
    }

    @Override
    protected String doInBackground(String... params) {
        byte[] result = null;
        String str = "";
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(params[0]);// in this case, params[0] is URL
        try {
            ArrayList<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
            Iterator<String> it = mData.keySet().iterator();
            while (it.hasNext()) {
                String key = it.next();
                nameValuePair.add(new BasicNameValuePair(key, mData.get(key)));
            }

            post.setEntity(new UrlEncodedFormEntity(nameValuePair, "UTF-8"));
            HttpResponse response = client.execute(post);
            StatusLine statusLine = response.getStatusLine();
            if(statusLine.getStatusCode() == HttpURLConnection.HTTP_OK){
                result = EntityUtils.toByteArray(response.getEntity());
                str = new String(result, "UTF-8");
            }
        }
        catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        catch (Exception e) {
            return null;
        }
        return str;
    }

    @Override
    protected void onPostExecute(String result) {
        try {
            JSONArray Loginjson = new JSONArray(result);
            strStt = Loginjson.getJSONObject(0).getJSONObject("fields").getString("status");
            if (strStt.equals("ERR")) {
                ErrorMsg("Authentication failed");
            } else if (strStt.equals("OK")) {
                ErrorMsg("Login OK!!!");
                ClientPage();
            } else {
                ErrorMsg("Connection Error");
            }
        } catch (JSONException e) {
            ErrorMsg("Connection Error");
        }       
    }
}

Now - i need to get another POST if the status is Error. do i need to make another Async class? with the same all procedures ? the issue is only the onPostExecute part is different.. actually the "doInBackground" will be always the same..

any idea how can i easily do multiple posts in the same activity?

Community
  • 1
  • 1
gabi
  • 1,003
  • 5
  • 12
  • 30

2 Answers2

0

Firstly, since your doInBackground() code will always stay the same, I recommend you move it into a general utility class.

Beyond that, you can go one of two ways:

  1. Create a new AsyncTask for each type of request that can call your utility class, and have its own onPostExecute()
  2. Create one AsyncTask that has a flag in it, which can be checked in the onPostExecute() method to see what code needs to be executed there. You will have to pass the flag in in the constructor or as a parameter in execute.
Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
  • how can i move the background to general utility class if i need it to be asynctask? the main thread cannot use network, no? also - can i just get the string from the response as return value and use it on the parent function instead the onPostExecute? – gabi Jun 22 '13 at 20:23
0

You can use a parameter at AsyncHttpPost constructor/execute or global variable to indicate if it is first or second POST (by other words - a flag). Then just create and execute another instance of AsyncHttpPost in onPostExecute (only if parameter/variable is set as "first POST").

Ilya Blokh
  • 11,923
  • 11
  • 52
  • 84
  • Got it, but i still have multiple classes of async extented in my activity, right? I have no way to do the onpostexecute on the main function? I mean if i could get the response back to the main i will do the checks there instead the onpostexecute... – gabi Jun 23 '13 at 05:23
  • No, you can have only one asynctask extended class. Pass a parameter into its constructor and execute different parts of code depending on this parameter. – Ilya Blokh Jun 23 '13 at 13:46