3

I want to create a connection between my Login Activity (email and password) and my local database (Xampp server) so I wrote this code.

The problem is that the application is not showing any errors or warnings but when I run it in the emulator it stops at some point, I checked many times and I found out that it doesn't get inside the "doInBackground" function. I don't know why

login_url="http://192.168.0.104/login.php"; is where my php file that contains the MySQL query.

here is my LoginActivity code :

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.androidnetworking.AndroidNetworking;

public class LoginActivity extends AppCompatActivity {

    public EditText EditTextEmail,EditTextPassword;
    public String Email, Password;

    private String ador;

    public CardView loginButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        AndroidNetworking.initialize(getApplicationContext());


        EditTextEmail =(EditText) findViewById(R.id.emailField);
        EditTextPassword =(EditText) findViewById(R.id.passwordField);

        loginButton = (CardView) findViewById(R.id.loginButton);

        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Email = EditTextEmail.getText().toString().trim();
                Password = EditTextPassword.getText().toString().trim();
                Toast.makeText(LoginActivity.this,Email,Toast.LENGTH_LONG).show();

                loginBackgroundWorker li = new loginBackgroundWorker(LoginActivity.this);
                li.execute(Email,Password);
            }
        });

    }
}

here is my loginBackgroundWorker class

import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

public class loginBackgroundWorker  extends AsyncTask<String,String,String> {
    Context context;

    public String email;
    public String password;

    public static final String login_url="http://192.168.0.104/login.php";


    public loginBackgroundWorker(Context context) {
        this.context = context;
    }

    @Override
    public void onPreExecute() {
        Toast.makeText(context,"preExecute",Toast.LENGTH_LONG).show();
    }

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

        Toast.makeText(context,"diInbackground",Toast.LENGTH_LONG).show();
        Toast.makeText(context,"diInbackground",Toast.LENGTH_LONG).show();

            try {
                email = params[0];
                password = params[1];
                URL url = new URL(login_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                OutputStream outputStream = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                String post_data = URLEncoder.encode("email","UTF-8")+"="+URLEncoder.encode(email,"UTF-8")+"&"
                        +URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
                bufferedWriter.write(post_data);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();


                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
                String result="";
                String line="";
                while((line = bufferedReader.readLine())!= null) {
                    result += line;
                }
                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
                return result;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        return null;
    }

    @Override
    public void onPostExecute(String result) {
        Toast.makeText(context,"Onpost",Toast.LENGTH_LONG).show();
        String r=result.trim();
        Boolean aBoolean = true;
        if (result.equals("1"))
        {
            Toast.makeText(context,"Bien Connecté",Toast.LENGTH_LONG).show();
           // Intent i = new Intent(context, MenuActivity.class);
            //i.putExtra("username",user_name);
           // context.startActivity(i);
            aBoolean =  false;
        }
        else if (result.equals(""))
            r = "Login ou mot de passe incorrect";

        if (aBoolean)
        {
            Toast.makeText(context,r,Toast.LENGTH_LONG).show();
        }

    }
}
Zoe
  • 27,060
  • 21
  • 118
  • 148
  • 1
    You need to look at [the stack trace](http://stackoverflow.com/questions/23353173) to determine the cause of the crash. I would point out that you cannot do a `Toast` directly from the `doInBackground()` method, so if you put those there for debugging purposes, you need to remove them before tracking down the real issue. Use log prints or breakpoints instead – Mike M. May 26 '18 at 02:51
  • 1
    Put breakpoint and debug the code, or use `Log.d` to log message and check the logcat instead of using `Toast` – Tam Huynh May 26 '18 at 02:53
  • Probably https://stackoverflow.com/questions/13790351/how-to-show-toast-in-asynctask-in-doinbackground. – ADM May 26 '18 at 04:26
  • Please remove `AndroidNetworking.initialize(getApplicationContext());` and then try. Normally the async task should work. – debo.stackoverflow May 26 '18 at 05:14
  • Possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Zoe May 27 '18 at 19:29

0 Answers0