0

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";
        }

    }
Learning2code
  • 51
  • 2
  • 11

1 Answers1

0

Long running task puts more load on main thread that's why NetworkOnMainThreadException showing, main thread is always used to perform small task like update UI etc. To Perform long running task use AsyncTask.

https://stackoverflow.com/a/6343299/4741746

This link show you how to use AsyncTask. in android

and after completing task if you want to update UI than you can use onPostExecute method of AsyncTask

also look at this post it help you -https://stackoverflow.com/a/12650928/4741746

Sushant Gosavi
  • 3,647
  • 3
  • 35
  • 55
  • Another method I tried was placing the network operation into another thread, and calling thread.start() in the main thread. It works this way, but is there any practice to this? – Learning2code May 25 '17 at 05:58
  • There are multiple ways like Handler , Thread , etc but best is AsyncTask – Sushant Gosavi May 25 '17 at 06:07
  • Most of use library like retrofit , volley, loopj async library etc in which retrofit is best – Sushant Gosavi May 25 '17 at 06:09
  • but ill suggest you to start with async task than move forward for best practice – Sushant Gosavi May 25 '17 at 06:10
  • Ahh I see. It seems my thread can only run twice, and anymore than that it will crash. May I ask for the AsyncTask, in my case, I only need to put the inputstream codes into the onPostExecute? My socket creation were created in another activity. – Learning2code May 25 '17 at 06:20
  • Yes you can use async task in this situation and put that inpute stream code in do in background method not in post execute ....do in background method work in background thread where as onPostExecute work on main thread refer -https://developer.android.com/reference/android/os/AsyncTask.html – Sushant Gosavi May 25 '17 at 06:35
  • If you don't mind, I added the AsyncTask I created. I am not very sure if it is correct. Please have a look. – Learning2code May 25 '17 at 06:54
  • Befor using Async Task please read all the details how it work - developer.android.com/reference/android/os/AsyncTask.html – Sushant Gosavi May 25 '17 at 08:33