0

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?

user3465277
  • 219
  • 3
  • 10

1 Answers1

0

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.

greywolf82
  • 21,813
  • 18
  • 54
  • 108
  • Hmm so what i am doing now is i am doing my work in second Thread but my UiThread is blocked. So i need to have second Thread and UiThread working in same time yes? – user3465277 Apr 12 '14 at 12:08
  • With the code posted I can't say where exactly is the problem I can only give you a tips about it. Maybe with more visibility of your code we could give you a best answer. – greywolf82 Apr 12 '14 at 12:23
  • I am trying to create new class now with extended Thread and there do my code meaby this way i wouldnt block my UiThread? – user3465277 Apr 12 '14 at 12:24
  • Maybe. The problem could even be in the send path. If you are sending an image I guess you are using a BitmapFactory method, it can require a "long" operation, so you have to check both the client and the server. Just an example http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html – greywolf82 Apr 12 '14 at 12:28
  • I will try to create new class just for bitmap oppreations , and i will extends there AsyncTask and do inBackgorund. – user3465277 Apr 12 '14 at 12:37