1
I have a problem in my application, when consuming a web service, the application is closing.
The idea of my application is I consume this web service to validate my login, and returning a message approving or not, I have no skill with Android, I’m recent in programming the same so I do not know if I do something right. I would like you to help me at 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 you to tell me if the method as I call the next screen after login is implemented in the best way. Below follows 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 is here
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 answer coming from the webservice, but there that is giving failure. 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)
Already took a look at the log to see the error message?
– Luídne
08:24:46 Gradle build finished in 16s 980ms 08:39:57 Gradle build finished with 5 error(s) in 9s 72ms 09:41:27 Gradle build finished in 2s 884ms
– Renan Rodrigues
I believe the requested log is what appears in logcat, where informs with more details the error occurred.
– Paulo Rodrigues
Actually the error here ta: Intent it = new Intent(Mainactivity.this, Home.class); startActivity(it); When I call the other screen, which correct way to do this ?
– Renan Rodrigues
Enter the class code Home also. The error must be in it.
– Luídne
Look, without knowing what the stack trace it gets really complicated to guess which error, so it is important to inform what is coming in the logcat.
– Paulo Rodrigues
the Home class has the code Hello Wold.
– Renan Rodrigues
The
NullPointerException
happens because its variableresult
is null at the moment you fulfill your conditions since the methodcallServer
is executed in another thread.– Paulo Rodrigues
how I’m supposed to fix this ?
– Renan Rodrigues
Thank you Paulo Rodrigues, to solve the problem ! Good afternoon.
– Renan Rodrigues