I am trying to receive data on my android phone, from the server. I do not understand why it always crashes... Please help!
seekbarbrightness.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekbarbrightness, int progress, boolean b) {
if(b==true) {
seekbarvalue = seekbarbrightness.getProgress();
multiplier = (double) seekbarvalue / 100;
finallumens = (int) (multiplier * LoginActivity.enterlumens);
tblumens.setText(String.valueOf(finallumens) + " Lumens");
if (LoginActivity.getSocket() != null) {
try {
InputStreamReader streamReader = new InputStreamReader(LoginActivity.getSocket().getInputStream());
BufferedReader reader = new BufferedReader(streamReader);
String str = reader.readLine();
tbvolts.setText("Voltage: "+str);
} catch (IOException e) {
e.printStackTrace();
}
}
else{
Toast.makeText(MainActivity.this, "NOT connected To Socket, please disconnect and reconnect!", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
If I remove the receive data codes, and place the following code in the try and catch block, it works.
LoginActivity.getSocket().getOutputStream().write(String.valueOf(multiplier).getBytes());
I'm not sure what I am doing wrong. But below is the error I get in logcat and my app just crashes.
Exception in MessageQueue callback: handleReceiveCallback.
android.os.NetworkOnMainThreadException
This is my Async Method I created. Is it correct? And any ideas how I could extract the str value in my AsyncTask and place it into a Textview? I can only Printout the value but not setting the text with it.
class ReceiveData extends AsyncTask<String, Void, String> {
protected String doInBackground(String... args) {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(LoginActivity.getSocket().getInputStream()));
in.readLine();
String str = in.readLine();
return str;
} catch (IOException e) {
e.printStackTrace();
return "fail";
}
}