Client Rest / Error in doInBackground () [closed]

0

Apparently there are no errors in the codes, but I do not know why it's pointing out an error in the //if (log.loginstatus(user,pass)=="1"); method. Except for error there is no method in doInBackGround that changes the thread . See:

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
    
asked by anonymous 17.08.2014 / 18:17

1 answer

1

I discovered the problem. I just needed to change the loginstatus method (by 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";
              }

    }
    
20.08.2014 / 03:04