Customer Rest/Erro no doInBackground()

Asked

Viewed 140 times

0

Apparently there are no errors in the codes, but I don’t know why you’re pointing out an error in the method //if (log.loginstatus(user,pass)=="1"); Unless mistaken there is no method in the doInBackGround amending the thread. Look at:

Class MainActivityLogin:

public class MainActivityLogin extends Activity {


    EditText user;
    EditText pass;
    Button bt_entrar;
    TextView registrar;
    Httppostaux post;
    String IP_Server="192.168.x.x:xxxx";
    String URL_connect="http://"+IP_Server+"/Elz";

    boolean result_back;


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




        user = (EditText) findViewById(R.id.id_edt_nome);
        user.setHint("Insira o usuário...");
        pass = (EditText) findViewById(R.id.id_edt_senha);
        pass.setHint("Insira a senha...");
        bt_entrar = (Button) findViewById(R.id.id_bt_entrar);
        post = new Httppostaux();


        bt_entrar.setOnClickListener(new View.OnClickListener(){

            public void onClick(View view){


                String usuario=user.getText().toString();
                String passw=pass.getText().toString();


                if( checklogindata( usuario , passw )==true){



                new Asy_task().execute(usuario,passw);                             


                }else{

                    err_login();
                }
            };
            }); //wtf?


        }


    public void err_login(){
        Vibrator vibrator =(Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(200);
        Toast toast1 = Toast.makeText(getApplicationContext(),"Error:Nome de usuario ou senha incorretos", Toast.LENGTH_SHORT);
        toast1.show();      
    }



    public String loginstatus(String username ,String password ) {

        int logstatus=-1;

        ArrayList<NameValuePair> postparameters2send= new ArrayList<NameValuePair>();

                    postparameters2send.add(new BasicNameValuePair("usuario",username));
                    postparameters2send.add(new BasicNameValuePair("password",password));


            JSONArray jdata=post.getserverdata(postparameters2send, URL_connect);



                if (jdata!=null && jdata.length() > 0){

                    JSONObject json_data; 
                    try {
                        json_data = jdata.getJSONObject(0); 
                         logstatus=json_data.getInt("logstatus");

                         Log.e("loginstatus","logstatus= "+logstatus);

                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }                   


                     if (logstatus==0){// [{"logstatus":"0"}]

                         Log.e("loginstatus ", "invalido");

                         return "0";
                     }
                     else{

                         Log.e("loginstatus ", "valido");

                         return "1";
                     }

              }else{    
                         Log.e("JSON  ", "ERROR");

                        return "0";
              }

    }



    public boolean checklogindata(String username ,String password ){

    if  (username.equals("") || password.equals("")){
        Log.e("Login ui", "checklogindata user or pass error");
    return false;

    }else{

        return true;
    }

}           
}    

Class Asy_task:

public class Asy_task extends AsyncTask <String, String, String> {

    boolean result_back;
    MainActivityLogin log;
    //private ProgressDialog pDialog;

    String user,pass;


    @Override
    protected String doInBackground(String... params) {

        user=params[0];
        pass=params[1];


        if (log.loginstatus(user,pass)=="1"){                       
            return "ok";
        }else{          
            return "err";  
        }




    }

    @Override
    protected void onPostExecute(String result) {

       //pDialog.dismiss();
       Log.e("onPostExecute=",""+result);

       if (result.equals("ok")){

            //Intent i=new Intent(this, Tela_01.class);
            //i.putExtra("user",user);
            //startActivity(i); 

        }
       if (result.equals("err")){


        log.finish();

       }

        }

}

Logcat error:

08-17 10:57:26.021: W/dalvikvm(1142): threadid=11: thread exiting with uncaught exception (group=0xb4a8dba8)
08-17 10:57:26.111: E/AndroidRuntime(1142): FATAL EXCEPTION: AsyncTask #1
08-17 10:57:26.111: E/AndroidRuntime(1142): Process: com.example.followjud, PID: 1142
08-17 10:57:26.111: E/AndroidRuntime(1142): java.lang.RuntimeException: An error occured while executing doInBackground()
08-17 10:57:26.111: E/AndroidRuntime(1142):     at android.os.AsyncTask$3.done(AsyncTask.java:300)
08-17 10:57:26.111: E/AndroidRuntime(1142):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
08-17 10:57:26.111: E/AndroidRuntime(1142):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
08-17 10:57:26.111: E/AndroidRuntime(1142):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
08-17 10:57:26.111: E/AndroidRuntime(1142):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
08-17 10:57:26.111: E/AndroidRuntime(1142):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
08-17 10:57:26.111: E/AndroidRuntime(1142):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
08-17 10:57:26.111: E/AndroidRuntime(1142):     at java.lang.Thread.run(Thread.java:841)
08-17 10:57:26.111: E/AndroidRuntime(1142): Caused by: java.lang.NullPointerException
08-17 10:57:26.111: E/AndroidRuntime(1142):     at com.example.followjud.Asy_task.doInBackground(Asy_task.java:31)
08-17 10:57:26.111: E/AndroidRuntime(1142):     at com.example.followjud.Asy_task.doInBackground(Asy_task.java:1)
08-17 10:57:26.111: E/AndroidRuntime(1142):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
08-17 10:57:26.111: E/AndroidRuntime(1142):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-17 10:57:26.111: E/AndroidRuntime(1142):     ... 4 more
  • Take a look at line 31 of your class Asy_task, there was a NPE there. How is setting the attribute log? At first you’re making use of it but I don’t see the attribution of it in your code.

  • Good evening, thank you for the answer. I’m trying to understand.. When you say (I don’t see the attribution of it in your code) are you referring to the function of the "loginstatus" method? Pq the log is the reference I gave the Mainactivitylogin class to call the method. I still can’t solve the T_T error

  • What I say is that at no time have I seen you set the variable log, and when calling the method loginstatus in a null object generates the error.

  • Aaaaa. I did the following then: protected void onPreExecute() { log = new Mainactivitylogin(); }/ Eh this?

  • It’s hard to find the solution.

1 answer

1

I discovered the problem. I just needed to change the method loginstatus (adding post = new Httppostaux();:

public String loginstatus(String username ,String password ) {



     int logstatus=-1;

        ArrayList<NameValuePair> postparameters2send= new ArrayList<NameValuePair>();

                    postparameters2send.add(new BasicNameValuePair("usuario",username));
                    postparameters2send.add(new BasicNameValuePair("password",password));


            JSONArray jdata= new JSONArray();

            Log.i("log_tag", "Error in http connection "+postparameters2send);

            post = new Httppostaux();

            jdata = post.getserverdata(postparameters2send, URL_connect);



                if (jdata!=null && jdata.length() > 0 ){

                    JSONObject json_data; 
                    try {
                        json_data = jdata.getJSONObject(0); 
                         logstatus=json_data.getInt("logstatus");

                         Log.e("loginstatus","logstatus= "+logstatus);

                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }                   


                     if (logstatus==0){// [{"logstatus":"0"}]

                         Log.e("loginstatus ", "invalido");

                         return "0";
                     }
                     else{

                         Log.e("loginstatus ", "valido");

                         return "1";
                     }

              }else{    
                         Log.e("JSON  ", "ERROR");

                        return "0";
              }

    }

Browser other questions tagged

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