Android app opens and soon stops working

Asked

Viewed 37 times

0

I want to create an application to trigger a rele from Arduino. I made the code but when I pass it to my mobile to test it gives the following error:

App Has Stopped Working

Follows the code.

MAIN ACTIVITY:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    Button btnon, btnoff;

    public String ipServ = "192.168.100.10";
    public String ptServ = "8888";
    public String URLCMD = "http://"+ipServ+":"+ptServ+"/?CMD=";
    public String URLLeitura = "http://"+ipServ+":"+ptServ;
    public String statusConexao="DESCONECTADO";
    public String[] valSensores=new String[5];
    int i=0;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnon = (Button)findViewById(R.id.btnon);
        btnoff = (Button)findViewById(R.id.btnoff);

        Cliente cliente =new Cliente(URLCMD+"STATUS");
        valSensores=cliente.getValSensores();
        decodificaLeitura(valSensores);
        statusConexao="CONECTADO";

        btnon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Cliente cliente =new Cliente(URLCMD+"LAMP01ON");
            }
        });

        btnoff.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Cliente cliente =new Cliente(URLCMD+"LAMP01OFF");
            }
        });
    }

    private void decodificaLeitura(String[] valSensores) {
    }
}

CUSTOMER CLASS:

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;

public class Cliente implements Runnable {
    String URL = "";
    public static String[] valSensores = new String[5];
    public static int i = 0;

    public Cliente(String Mensagem) {
        super();
        URL = Mensagem;
        Thread msg = new Thread(this);
        msg.start();
    }

    public void fim() {
    }

    @Override
    public void run() {
        try {
            HttpClient cliente = new DefaultHttpClient();
            HttpGet solicitar = new HttpGet();
            solicitar.setURI(new URI(URL));
            HttpResponse resposta = cliente.execute(solicitar);
            BufferedReader leitor = new BufferedReader(new InputStreamReader(resposta.getEntity().getContent()));
            String linha = "";
            while ((linha = leitor.readLine()) != null)
                if (linha != null) {
                    valSensores[i] = linha;
                    i = i + 1;
                }
            leitor.close();
            i = 0;
        } catch (URISyntaxException e) {

            e.printStackTrace();
        } catch (IllegalStateException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }

    }

    public static String[] getValSensores() {
        return valSensores;
    }
}

1 answer

0

I believe that in this line

    Cliente cliente =new Cliente(URLCMD+"STATUS");
    valSensores=cliente.getValSensores(); <-----
    decodificaLeitura(valSensores);
    statusConexao="CONECTADO";

the program conflicts because this value is not stored. (the constructor of the Client class fills in the attribute "URL", not "valSensores")

  • The legal, I will change then. Thank you very much Alberto

  • but then it worked out?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.