【问题标题】:How can I start AsyncTask's doInBackground repeatedly when I start the 'A activity'? [complete]启动“A 活动”时,如何重复启动 AsyncTask 的 doInBackground? [完全的]
【发布时间】:2016-02-11 02:38:14
【问题描述】:

我使用 HttpURLConnection 在我的 android 的 AsyncTask 中从 MySQL (android+jsp+MySQL) 获取数据。 当我开始“一项活动”时,第一个就可以了。我可以开始 doInBackground。但是当我接下来启动“A 活动”时,当我重复启动“A 活动”时,我无法启动 doInBackgound。

我想在重复启动“A 活动”时启动 doInBackground 因为我在 doInBackground 中从 MySQL 获取数据。

我曾经使用“task.cancel(true)”,但这不起作用。

我是安卓新手,请告诉我如何重复启动doInBackground。

提前感谢。

后面是我的代码。

关于创建代码

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_confirm_inventory);
...........
    connectJSP = new getInventoryFromMySQL();
    connectJSP.execute();
...........
}

onBackPressed 代码

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.END)) {
        drawer.closeDrawer(GravityCompat.END);
    } else {
        connectJSP.cancel(true);
        super.onBackPressed();
    }
}

异步任务代码

private class getInventoryFromMySQL extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... sId) {
        String sResult = "Error";

        try {
            //URL setting and access
            URL url = new URL("http://-----.com/*****.jsp");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            //setting
            conn.setRequestMethod("POST");

            // connection values
            String sendBicycleName = bicycleName;
            String sendBicycleYear = bicycleYear;

            //StringBuffer
            StringBuffer buffer = new StringBuffer();
            buffer.append("sendBicycleName").append("=").append(sendBicycleName).append("&");
            buffer.append("sendBicycleYear").append("=").append(sendBicycleYear);

            //put data into JSP
            OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            osw.write(buffer.toString());
            osw.flush();

            //get data from JSP
            InputStreamReader tmp = new InputStreamReader(conn.getInputStream(), "UTF-8");
            BufferedReader reader = new BufferedReader(tmp);
            String str;

            //fit the order with JSP(garbage values)
            reader.readLine(); reader.readLine(); reader.readLine(); reader.readLine();
            //get data from JSP
            for(;;) {
                if((str = reader.readLine()) != null && (str != "") && (str != " ") && (str != "null")) {
                    mysqlStoreId[countInventory] = str;
                    for(int c=0; c<5; c++) {
                        for(int s=0; s<8; s++) {
                            str = reader.readLine();
                            mysqlInventory[countInventory][c][s] = Integer.parseInt(str);
                        }
                    }
                    countInventory++;
                } else if(str == null && str == "null") {
                    //finish for if values equals null
                    break;
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sResult;
    }
}

【问题讨论】:

    标签: android-asynctask httpurlconnection


    【解决方案1】:

    我找到了答案。我检查所有代码的日志。 问题是“doInBackground”有一个错误。 'for(;;)' 中没有 else,所以 AsyncTack 生成 'onCancelled()'。但我没有编写“onCancelled()”代码。

    当我添加 onCancelled() 时,我可以取消(true) 并重新启动 Asynctask。

    谢谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多