Error on Android

Asked

Viewed 36 times

-1

I am creating a login system on Android, but for some reason is giving me this error:

Skipped 280 frames! The application may be Doing Too Much work on its main thread.

Can someone help me?

package imm.pt.immsmart;

    import android.content.Intent;
    import android.graphics.Typeface;
    import android.os.StrictMode;
    import android.support.v7.app.AlertDialog;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.TextView;

    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.util.EntityUtils;

    import java.io.IOException;
    import java.util.ArrayList;

    public class Login extends AppCompatActivity {
        public void entrar(View view){

                    TextView user = (TextView) findViewById(R.id.mail);
                    TextView pass = (TextView) findViewById(R.id.password);
                    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                    StrictMode.setThreadPolicy(policy);
                    HttpClient httpClient = new DefaultHttpClient();
                    HttpPost httpPost = new HttpPost("http://192.168.1.66/immsmart/api.php");
                    try {
                        ArrayList<NameValuePair> valores = new ArrayList<NameValuePair>();
                        valores.add(new BasicNameValuePair("task", "login"));
                        valores.add(new BasicNameValuePair("mail", user.getText().toString()));
                        valores.add(new BasicNameValuePair("pass", pass.getText().toString()));
                        httpPost.setEntity(new UrlEncodedFormEntity(valores));
                        final HttpResponse resposta = httpClient.execute(httpPost);
                        String res = EntityUtils.toString(resposta.getEntity());
                        if(res.equals("notfound")) {
                            user.setText("");
                            pass.setText("");
                            AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
                            builder.setMessage("Dados Invalidos!");
                            builder.create();
                        }else{
                            AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
                            builder.setMessage(res);
                            builder.create();
                            Intent mainIntent = new Intent(Login.this, Menu.class);
                            Login.this.startActivity(mainIntent);
                            Login.this.finish();
                        }
                    }catch (ClientProtocolException e){} catch (IOException e) {}
        }
    }

1 answer

3


Since you’re doing a request on the main thread, it prioritizes this by rendering the screen, skipping frames, this causes this alert to appear

It is recommended not to use the main thread to make requests for this and several other reasons.

There are some good libraries that simplify the control of requests with Volley (today maintained by google https://github.com/google/volley) and the retrofit (https://github.com/square/retrofit)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.