1

I am following a tutorial which adds data to remote sql database, I get the above error and on some occasions the "endAllStagingAnimators" errors,The app uses AsyncTask so how come it still gives the above error??

MainActivity:

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));
    }
}

AsyncTask class BackgroundTask:

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);
    }
}

Register Class:

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();
    }
}

Logcat:

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.

Manifest File:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="hilz.mysqldemo" >

    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".Register" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category." />
            </intent-filter>
        </activity>
    </application>
</manifest>
Mike
  • 4,550
  • 4
  • 33
  • 47
Hilary Mwape
  • 425
  • 1
  • 4
  • 15
  • http://stackoverflow.com/questions/14678593/the-application-may-be-doing-too-much-work-on-its-main-thread – JoCuTo Dec 18 '15 at 14:49
  • where you are calling `userReg` in `Register` – Iamat8 Dec 18 '15 at 14:52
  • I have Read that,And amusing AsyncTask already thats why i ask because i still cant figure it out,Am a beginner – Hilary Mwape Dec 18 '15 at 14:53
  • @Mohit It seemed to work in tutorial i saw,What alterative is there to this,where else should this take place?? – Hilary Mwape Dec 18 '15 at 14:55
  • 3
    Use a profiler (e.g., method tracing in Android Studio) to determine where you are spending your time on the main application thread. Also, consider playing around with `StrictMode` to have it log problems related to I/O on the main application thread. 312 frames is about five seconds; you should not have a problem identifying where your code is spending five seconds without returning control of the main application thread to the framework. – CommonsWare Dec 18 '15 at 14:56
  • Its probably where i call userReg in Reister Class,But what would be the correct way to sort that out?? i have no idea how,@CommonsWare – Hilary Mwape Dec 18 '15 at 15:00
  • I do not see where your calling `userReg ` – Iamat8 Dec 18 '15 at 15:01
  • @Mohit Its been set as an onClick method in both activity_main.xml Button, and also in register.xml Button – Hilary Mwape Dec 18 '15 at 15:06
  • try calling `Async` at `onCreate` – Iamat8 Dec 18 '15 at 15:09
  • Does that mean i move all code under userReg to onCreate ?? – Hilary Mwape Dec 18 '15 at 15:17
  • @CommonsWare, How to use Profiler in android studio to check the amount of time spend on particular thread? – Pankaj Nimgade Dec 18 '15 at 15:31
  • BTW, it's unnecessary and pointless to override a method if all you do is call super. – Kevin Krumwiede Dec 19 '15 at 15:20
  • Yes but i will use it shortly am just at a phase where i am not using it currently,so its there to remind me – Hilary Mwape Dec 19 '15 at 15:21

2 Answers2

0

Why you destroy de activity with finish(); after invoke the task? If you do this, you could have a lot of unexpected errors.

0

Apologise for the Harvoc caused,They where two things wrong with code,First i was using the Gateway IP 192.168.1.1, which when i was in a hurry thought was my PC ip,second is on the Toast method,didnt show it so i couldnt see that data was inserted,None of you guys noticed either,But my fault should have checked better,Thaks for help

Hilary Mwape
  • 425
  • 1
  • 4
  • 15