I have a problem in my application, when consuming a web service service, the application is closing.
The idea of my application is that I consume this web service to validate my login, and returning a message approving or not, I do not have Android skills, I'm new to programming it so I do not know if I'm doing something right. I would like you to help me through this stage of the project.
This code refers to MainActivity.java
package com.example.hotsystems.hs_celulas;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends Activity {
String asw;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hs_cell);
//Chamar os objetos
final EditText email = (EditText) findViewById(R.id.TXT_EMAIL_LOGIN);
final EditText senha = (EditText) findViewById(R.id.TXT_SENHA_LOGIN);
final EditText cod_igrej = (EditText) findViewById(R.id.COD_IGREJ_LOGIN);
Button entrar = (Button) findViewById(R.id.BTN_ENTRA_APLIC);
Button sair = (Button) findViewById(R.id.BTN_SAIRX_APLIC);
entrar.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
Usuario usr = new Usuario();
String json = generateJson(usr);
String result = callServer("send-json", json);
if(result.equals("3")){
Log.i("MainActivity", result);
//Toast.makeText(MainActivity.this, "Código da igreja inesistente, por favor escreva um código válido." , Toast.LENGTH_SHORT).show();
}else if (result.equals("2")){
Log.i("MainActivity", result);
//Toast.makeText(MainActivity.this, "Senha incorreto, por favor escreva uma senha válida." , Toast.LENGTH_SHORT).show();
}else if (result.equals("1")){
Log.i("MainActivity", result);
//Toast.makeText(MainActivity.this, "E-mail incorreto, por favor escreva um email válido." , Toast.LENGTH_SHORT).show();
}else{
Intent it = new Intent(MainActivity.this, Home.class);
startActivity(it);
}
}
});
sair.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
finish();
}
});
}
public String generateJson(Usuario usr){
final EditText email = (EditText) findViewById(R.id.TXT_EMAIL_LOGIN);
final EditText senha = (EditText) findViewById(R.id.TXT_SENHA_LOGIN);
final EditText cod_igrej = (EditText) findViewById(R.id.COD_IGREJ_LOGIN);
JSONObject jo = new JSONObject();
String strEmail = email.getText().toString();
String strSenha = senha.getText().toString();
String strCod = cod_igrej.getText().toString();
try{
jo.put("email", strEmail);
jo.put("senha", strSenha);
jo.put("cod_igrej", strCod);
}catch (JSONException e){
e.printStackTrace();
}
return(jo.toString());
}
public Usuario degenerateJson(String data){
Usuario usr = new Usuario();
try{
JSONObject jo = new JSONObject(data);
usr.setEmail(jo.getString("email"));
usr.setEmail(jo.getString("senha"));
usr.setEmail(jo.getString("cod_igrej"));
/*jo.put("email", Usuario.getEmail());
jo.put("senha", Usuario.getSenha() );
jo.put("cod_igrej", Usuario.getCod());*/
}catch (JSONException e){
e.printStackTrace();
}
return(usr);
}
private String callServer(final String method, final String data){
new Thread(){
public void run(){
asw = Connection.getSetDataWeb("http://192.168.1.20/renan/process.php", method, data);
if (data.isEmpty()){
degenerateJson(asw);
}
}
}.start();
return asw;
}
}
I would also like to be told if the method as I call the next screen after login is implemented in the best way. Below is my web server service.
<?php
require_once('Usuario.php');
include('conexao.php');
if(strcmp('send-json', $_POST['method']) == 0)
{ // SEND
$Usuario = utf8_encode($_POST['json']);
$Usuario = json_decode($Usuario);
$email = $Usuario->email;
$senha = $Usuario->senha;
$cod_igrej = $Usuario->cod_igrej;
$sql = "SELECT COD_IDENT_IGREJ, TXT_NOMEX_IGREJ, FLG_STATU_IGREJ FROM tbl_IGREJAS WHERE COD_IDENT_IGREJ = '".$cod_igrej."'";
$sql2 = "SELECT COD_IDENT_USUAR, TXT_EMAIL_LOGIN, TXT_SENHA_LOGIN FROM tbl_USUARIOS WHERE TXT_EMAIL_LOGIN = '".$email."'";
$resultado = mysql_query($sql);
if (mysql_num_rows($resultado) == 0)
{
$val= '003';
}else
{
$resultado = mysql_query($sql2);
if (mysql_num_rows($resultado) == 0 )
{
$val='001';
}else
{
$usuario = mysql_fetch_object($resultado);
if ($senha == $usuario->TXT_SENHA_LOGIN) {
$val='000';
}else{
$val= '002';
}
}
}
switch ($val)
{
case '001':
/*E-mail incorreto, por favor escreva um email válido.*/
echo '1';
break;
case '002':
/*Senha incorreta, por favor escreva uma senha válido.*/
echo '2';
break;
case '003':
/*Codigo da igreja inesistente, por favor escreva um codigo válido.*/
echo '3';
break;
default:
/*Login efetuado com sucesso*/
echo '0';
break;
};
}
else if(strcmp('get-json', $_POST['method']) == 0)
{ // GET
$Usuario = new Usuario();
$Usuario->setEmail(utf8_encode('Renan'));
$Usuario->setSenha('12345');
$Usuario->setCod('001');
echo json_encode($Usuario->getDataJSON());
}
? >
The basic error here is the
switch (result){
case "3":
Toast.makeText(MainActivity.this, "Código da igreja inesistente, por favor escreva um código válido." , Toast.LENGTH_SHORT).show();
break;
case "2":
Toast.makeText(MainActivity.this, "Senha incorreto, por favor escreva uma senha válida." , Toast.LENGTH_SHORT).show();
break;
case "1":
Toast.makeText(MainActivity.this, "E-mail incorreto, por favor escreva um email válido." , Toast.LENGTH_SHORT).show();
break;
default:
Intent it = new Intent(MainActivity.this, Home.class);
startActivity(it);
}
I need to compare with the response coming from the webservice, but then that is giving the flaw. Logcat is:
java.lang.NullPointerException
at com.example.hotsystems.hs_celulas.MainActivity$1.onClick(MainActivity.java:40)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)