I want to create an application to trigger an arduino relay. I made the code but when I pass it to my cell phone to test it it gives the following error:
App Stopped Working
Follow 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) {
}
}
CLASS 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;
}
}