简体   繁体   中英

When trying to send a command to the web server using java i'm getting NetworkOnMainThreadException. How can i fix it?

The exception happen when i'm unchecking the checkbox and it's trying to call the Get method. Not sure how to handle/fix it.

In the MainActivity.java i have checkbox and addListenerOnCheckBoxMute:

public void addListenerOnCheckBoxMute(){
        checkboxMute.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (((CheckBox) v).isChecked()) {
                    timerValueUpload.setText("Mute");
                }
                else
                {
                    timerValueUpload.setText("Unmute");
                    checkMute = Get(iptouse + "mutestatus");
                }
            }
        });
    }

And the Get method:

private byte[] Get(String urlIn)
    {
        URL url = null;
        String urlStr = urlIn;
        if (urlIn!=null)
            urlStr=urlIn;
        try
        {
            url = new URL(urlStr);
        } catch (MalformedURLException e)
        {
            e.printStackTrace();
            return null;
        }
        HttpURLConnection urlConnection = null;
        try
        {
            urlConnection = (HttpURLConnection) url.openConnection();
            InputStream in = new BufferedInputStream(urlConnection.getInputStream());
            byte[] buf=new byte[10*1024];
            int szRead = in.read(buf);
            byte[] bufOut;
            if (szRead>=10*1024)
            {
                throw new AndroidRuntimeException("the returned data is bigger than 10*1024.. we don't handle it..");
            }
            else
            {
                if (szRead>0)
                {
                    bufOut = Arrays.copyOf(buf, szRead);
                }
                else
                {
                    bufOut=null;
                }
            }
            return bufOut;
        }
        catch (IOException e)
        {
            e.printStackTrace();
            return null;
        }
        finally
        {
            if (urlConnection!=null)
            {
                urlConnection.disconnect();
            }
        }
    }

Log :

11-21 02:07:38.001    4633-4633/com.test.webservertest E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.test.webservertest, PID: 4633
    android.os.NetworkOnMainThreadException
            at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
            at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:249)
            at libcore.io.IoBridge.recvfrom(IoBridge.java:553)
            at java.net.PlainSocketImpl.read(PlainSocketImpl.java:485)
            at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:37)
            at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:237)
            at com.android.okio.Okio$2.read(Okio.java:113)
            at com.android.okio.RealBufferedSource.indexOf(RealBufferedSource.java:147)
            at com.android.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:94)
            at com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:175)
            at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:101)
            at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:616)
            at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:379)
            at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:323)
            at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:190)
            at com.adilipman.webservertest.MainActivity.Get(MainActivity.java:807)
            at com.adilipman.webservertest.MainActivity.access$300(MainActivity.java:81)
            at com.adilipman.webservertest.MainActivity$1.onClick(MainActivity.java:264)
            at android.view.View.performClick(View.java:4763)
            at android.widget.CompoundButton.performClick(CompoundButton.java:125)
            at android.view.View$PerformClick.run(View.java:19821)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5274)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)

This exception is thrown when you attempts to perform a networking operation on the application main thread. Move your code in AsyncTask.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM