I'm developing an application, and I'm not getting the login screen to verify that the fields are being filled or if they have the wrong username and password. Here is the login code.
It logs normal, the problem is when I log in without filling in the fields or with the wrong username or password.
package com.rafaeljacinto.newtest4;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class Telalogin extends AppCompatActivity {
EditText name, password;
Button login;
String Name, Password;
Context ctx=this;
String codAlu =null, nomeAlu = null, emailAlu = null, foneAlu = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_telalogin);
name = (EditText) findViewById(R.id.name);
password = (EditText) findViewById(R.id.password);
login = (Button)findViewById(R.id.login);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Name = name.getText().toString();
Password = password.getText().toString();
BackGround b = new BackGround();
b.execute(Name, Password);
}
});
}
class BackGround extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
String name = params[0];
String password = params[1];
String data="";
int tmp;
try {
URL url = new URL("http://192.168.0.12/ProjetoNewWebService/Login.php");
String urlParams = "name="+name+"&password="+password;
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoOutput(true);
OutputStream os = httpURLConnection.getOutputStream();
os.write(urlParams.getBytes());
os.flush();
os.close();
InputStream is = httpURLConnection.getInputStream();
while((tmp=is.read())!=-1){
data+= (char)tmp;
}
is.close();
httpURLConnection.disconnect();
return data;
} catch (MalformedURLException e) {
e.printStackTrace();
return "Exception: "+e.getMessage();
} catch (IOException e) {
e.printStackTrace();
return "Exception: "+e.getMessage();
}
}
@Override
protected void onPostExecute(String s) {
String err=null;
try {
JSONObject jo = new JSONObject(s);
JSONObject dados = jo.getJSONObject("usuario");
codAlu = dados.getString("codAlu");
nomeAlu = dados.getString("nomeAlu");
emailAlu = dados.getString("emailAlu");
foneAlu = dados.getString("foneAlu");
} catch (JSONException e) {
e.printStackTrace();
err = "Exception: "+e.getMessage();
}
//Toast.makeText(TelaLogin.this, s,Toast.LENGTH_SHORT).show();
Intent in = new Intent(Telalogin.this,Teladisciplinas.class);
in.putExtra("codAlu",codAlu);
in.putExtra("nomeAlu",nomeAlu);
in.putExtra("emailAlu",emailAlu);
in.putExtra("foneAlu",foneAlu);
startActivity(in);
}
}
}