0

我写了这些线程:
如何在 Android 中有效地管理多个异步任务
同时运行多个异步任务——不可能?

但没有找到我的问题的答案,也许有人可以帮助..

我有 android 应用程序可以进行登录 POST 并获得 json 响应,
如果 Json 正常,我需要发布另一个数据以获得另一个响应。

我已经扩展了 Async 类,该类将发布到 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");
        }       
    }
}

现在 - 如果状态为错误,我需要获取另一个 POST。我需要制作另一个 Async 类吗?所有程序都一样吗?问题只是 onPostExecute 部分不同..实际上“doInBackground”将始终相同..

知道如何在同一个活动中轻松发布多个帖子吗?

4

2 回答 2

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.
于 2013-06-22T19:42:52.333 回答
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").

于 2013-06-22T19:45:06.360 回答