TCP client in Android: receive message, display image

Put your problem here if it does not fit any of the other categories.

TCP client in Android: receive message, display image

Postby madhunandan » Wed Jan 26, 2011 12:15 am


I have a TCP client running in Android phone. It connects and receives data from a windows application. Once I receive a message, I parse it and I try to display an image. I have to keep checking for new messages from this windows app.

The client thread keeps running. When a message is received, I pass it to the main activity, parse it but I am not able to display the image. I want to make the client check for new message every 100 ms. At the moment, the LogCat gets flooded as the thread keeps running and I am not able to really see the contents of LogCat.

Basically I want to run the client, keep checking for new messages every 100 ms, when a new message is there, pass it to the main activity, parse it and display an image. Please go through the code below and suggest any corrections or a better approach if necessary.



The client code is below.

Main activity:

Code: Select all
public class TCPListen extends Activity implements TCPListener {
   private TextView mTitle;
   public String data[] = new String[2];
    /** Called when the activity is first created. */
        public void onCreate(Bundle savedInstanceState) { 
         // Set up the window layout
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);

            // Set up the custom title
            mTitle = (TextView) findViewById(;
            mTitle = (TextView) findViewById(;
            //TcpServiceHandler handler=new TcpServiceHandler(this); 
            TcpServiceHandler handler = new TcpServiceHandler(this,this); 
            Thread th = new Thread(handler); 
        public String[] callCompleted(String source){
              Log.d("TCP", "Std parser " + source);
              //String data[] = new String[2];
              //if (source.matches("<MSG><N>.*</N><V>.*</V></MSG>"))   {           
                  Document doc = null; 
                    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
                     DocumentBuilder db = dbf.newDocumentBuilder(); 
                     doc = (Document) db.parse(new ByteArrayInputStream(source.getBytes())); 
                     NodeList n = doc.getElementsByTagName("N"); 
                     Node nd = n.item(0); 
                     String msgName = nd.getFirstChild().getNodeValue(); 
                     NodeList n1 = doc.getElementsByTagName("V"); 
                     Node nd1 = n1.item(0); 
                     String tmpVal = nd1.getFirstChild().getNodeValue(); 
                     data[0] = msgName; 
                     data[1] = tmpVal;
                     Log.d("TCP", "Inside Std parser " + data[0] + " " + data[1]);
                     actionOnData(data[0], data[1]);
                  catch(Exception e){ 
              Log.d("TCP", "Just outside Std parser " + data[0] + " " + data[1]);
              return data;
              //} else Log.d("TCP", "Message in wrong format " + source);
              //mTitle.setText("Message in wrong format " + source);
              //return data;
      //Function to display driver messages/images based on individual messages
        public void actionOnData(String name, String value) {
           String tempName = name;
           String tempVal = value;             
           //while (true)   {
              if(tempName.equals("shiftDirection") && tempVal.equals("1"))   {
                 Log.d("TCP","in actionOnData " + data[0] + " " + data[1]);
                 mTitle.setText("Change to next higher gear");
                   Intent myIntent = new Intent();
                   myIntent.setClassName("", "");
                   //myIntent.putExtra("Change gear", "Shift to next gear!"); // key/value pair, where key needs current package prefix.
                   try {
                    } catch(InterruptedException e) {
                    System.out.println("InterruptedException caught");
               } else if(tempName.equals("vehicleSpeed") && tempVal.equals("120"))   {
                 Log.d("TCP","in actionOnData " + data[0] + " " + data[1]);
                 mTitle.setText("Drive like a man");
                   //Intent myIntent = new Intent();
                   //myIntent.setClassName("", "");
                   ////myIntent.putExtra("Change gear", "Shift to next gear!"); // key/value pair, where key needs current package prefix.
               } else Log.d("TCP", "Just show an image");


Code: Select all
public interface TCPListener {
   public String[] callCompleted(String msg);

Client thread:

Code: Select all
public class TcpServiceHandler implements Runnable { 
     TCPListener _listener;           
     private Activity _act; 
     public TcpServiceHandler(TCPListener listener, Activity act){   
         _listener = listener; 
         _act = act; 
     public synchronized void run() { 
         // TODO Auto-generated method stub           
             try { 
                 //InetAddress serverAddr = InetAddress.getByName(""); 
                 Socket socket = new Socket("", 1200, true); 
                  try {                           
                         BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
                         final String str = in.readLine(); 
                         this._act.runOnUiThread(new Runnable(){ 
                         public void run() { 
                  catch(Exception e){ 
             } catch (UnknownHostException e) { 
                 // TODO Auto-generated catch block 
             } catch (IOException e) { 
                 // TODO Auto-generated catch block 
User avatar
Junior Developer
Junior Developer
Posts: 10
Joined: Wed Dec 08, 2010 4:40 pm
Location: Rüsselsheim, Germany


Return to Other Coding-Problems

Who is online

Users browsing this forum: Yahoo [Bot] and 16 guests