使用AsyncTask但仍然在主线程错误上得到太多的工作

时间:2015-12-18 14:48:24

标签: java android android-asynctask

我正在关注一个将数据添加到远程sql数据库的教程,我得到了上述错误,并且在某些情况下" endAllStagingAnimators"错误,该应用程序使用AsyncTask所以它怎么仍然给出上述错误??

MainActivity:

data2 = {h:v for h,v in zip (str(xrange(16)), zip(*values))}
df2 = pd.DataFrame.from_dict(data)

AsyncTask类BackgroundTask:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void userReg(View view) {
        startActivity(new Intent(this,Register.class));
    }
}

注册类:

public class BackgroundTask extends AsyncTask{

    Context context;
    BackgroundTask(Context context) {
        this.context= context;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Object doInBackground(Object[] params) {
        String url_reg= "http://192.168.1.1/webapp/register.php";
        String url_login= "http://192.168.1.1/webapp/login.php";

        String method= (String) params[0];
        if (method.equals("register")) {
            String name= (String) params[1];
            String user_name= (String) params[2];
            String user_pass= (String) params[3];

            try {
                URL url= new URL(url_reg);
                HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                OutputStream os= httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter= new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
                String Data=(URLEncoder.encode("user", "UTF-8") + "=" + URLEncoder.encode(name, "UTF-8") + "&" +
                       URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(user_name, "UTF-8") +
                       URLEncoder.encode("user_pass", "UTF-8") + "=" + URLEncoder.encode(user_pass, "UTF-8"));
                bufferedWriter.write(Data);
                bufferedWriter.flush();
                bufferedWriter.close();
                os.close();
                InputStream is= httpURLConnection.getInputStream();
                is.close();
                return "Registration Success";
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Object[] values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(Object o) {
        Toast.makeText(context,o.toString(),Toast.LENGTH_LONG);
    }
}

logcat的:

public class Register extends Activity {
    EditText et_name,et_user_name,et_user_pass;
    Button btn_register;
    String name,userName,userPass;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_layout);
        et_name= (EditText) findViewById(R.id.et_regname);
        et_user_name= (EditText) findViewById(R.id.et_regusername);
        et_user_pass= (EditText) findViewById(R.id.et_regpass);
        btn_register= (Button) findViewById(R.id.btn_register);
    }

    public void userReg(View view) {
        name= et_name.getText().toString();
        userName= et_user_name.getText().toString();
        userPass= et_user_pass.getText().toString();
        String method= "register";

        BackgroundTask backgroundTask= new BackgroundTask(this);
        backgroundTask.execute(method,name,userName,userPass);
        finish();
    }
}

清单文件:

12-18 16:35:49.818 1964-1980/hilz.mysqldemo W/EGL_emulation: eglSurfaceAttrib not implemented
12-18 16:35:49.818 1964-1980/hilz.mysqldemo W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa502f580, error=EGL_SUCCESS
12-18 16:35:55.058 1964-1964/hilz.mysqldemo I/Choreographer: Skipped 312 frames!  The application may be doing too much work on its main thread.

2 个答案:

答案 0 :(得分:0)

为什么用finish()销毁de活动;调用任务后?如果这样做,您可能会遇到很多意外错误。

答案 1 :(得分:0)

为Harvoc造成的道歉,他们在哪里有两个错误的代码,首先我使用的是网关IP 192.168.1.1,当我匆忙的时候认为是我的PC ip,第二个是Toast方法,没有显示所以我无法看到插入的数据,你们其中没有人注意到,但我的错误应该更好地检查,他们寻求帮助