简体   繁体   中英

App not responding while transfer data

I am sending image using Socket Server and Client. It gived me dialog "App not responding" i think beacuse converting this bitmap was making in UiThread. So i tried to change it but i am still getting this message "App is not responding". It's happening when i am sending big imaes +500kb.

Here is my code for Server:

public class SocketServerThread extends Thread {

      static final int SocketServerPORT = 8080;
      int count = 0;

      @Override
      public void run() {
       try {
        serverSocket = new ServerSocket(SocketServerPORT);               
        while (true) {
         Socket socket = serverSocket.accept();
         count++;

         // Here where i am doing my code i think is not doing in UiThread..

         MainActivity.this.runOnUiThread(new Runnable() {
          @Override
          public void run() 
          {                 
          // Firstly i was doing my code here...
          }
         });

        }
       } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }
      }

     }

My code for client:

public class MyClientTask extends AsyncTask<Void, Void, Void> {   

      MyClientTask(String addr, int port){
       dstAddress = addr;
       dstPort = port;
      }

    @Override
    protected Void doInBackground(Void... arg0) {

           Socket socket = null;

           try 
                        {
    //I am sending my image here... 

            }
        }
           } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            response = "UnknownHostException: " + e.toString();
           } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            response = "IOException: " + e.toString();
           }finally{
            if(socket != null){
             try {
              socket.close();
             } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
             }
            }
           }
           return null;
          }
          @Override
          protected void onPostExecute(Void result) 
          {
           super.onPostExecute(result);
          }
    }

So please help my . Why still i am getting not responding dialog?

The ANR error code happens when you block the UI thread more than 5 seconds. If you need to do background work don't use the main thread. Receive the data in a separate thread and post only the result to the UI thread.

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