Question

I need to use the tcp socket connection to get the data from a bluebox, if I input a comment, such as "getcolor", the bluebox will send me the information like"red, blue". In this case the bluebox as a server and I do not need to program on it, but I have problem to show the information on the EditText.

public class sender {
public static void main(String[] args)throws IOException{

        Socket socket = new Socket("192.168.1.176",14111);

        OutputStream out = socket.getOutputStream();

        BufferedReader msg = new BufferedReader(new InputStreamReader(System.in)); 
        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));         
         PrintWriter ou = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out)),true);

         String buffer = new String("");
         String ms = "";
      while(true)
      {
          while(in.ready())

              buffer+= in.readLine()+ "\n";
          String[] line = buffer.split("\n");

          while(msg.ready())
              ms = msg.readLine();
          if(ms.equals("exit"))
          {
              break;
          }
          if(!ms.equals(""))
          {
              ou.println(ms);
              ou.flush();
              ms = "";
          }
          if(!buffer.equals(""))
          {

              System.out.print(buffer);
              buffer = "";

          }

      } 

      in.close();
      out.close();
      socket.close();

}
}

this java code works, but it fails in the android code below:

public class BlueBoxApp extends Activity {
/** Called when the activity is first created. */

Context appInstance = this;
private EditText info;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
           info = (EditText)findViewById(R.id.EditText01);
    try{

        InetAddress serverAddr = InetAddress.getByName("192.168.1.176");//TCP服务器IP地址

            Log.d("TCP", "server,receiving...");

          Socket socket = new Socket(serverAddr,14111);


           try {   


           OutputStream out = socket.getOutputStream();
           BufferedReader msg = new BufferedReader(new InputStreamReader(System.in)); 
           BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));          
         PrintWriter ou = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out)),true);

            String buffer = new String("");
            String ms = "getsensorno";
            Log.d("TCP", "sending:'"+ms+"'");
        while(true)
        {
          while(in.ready())

              buffer+= in.readLine()+ "\n";

          while(msg.ready())
              ms = msg.readLine();
          if(ms.equals("exit"))
          {
              break;
          }
          if(!ms.equals(""))
          {
              ou.println(ms);
              ou.flush();
              ms = "";
          }
          if(!buffer.equals(""))
          {

             info.setText(buffer);
            buffer = "";

          } 
        } 

            } catch (Exception e) {

            Log.e("TCP", "error",e);

        }finally{

            socket.close();

        }

        }catch(Exception e){

           Log.e("TCP", "error",e);

        }
}
}

what is the problem and how to set a thread for it? Thanks!

Was it helpful?

Solution

The problem, as you indicated, is that you are doing the networking part on the main thread.

Setting a new thread is easy, consider using AsyncTask. Please read the documentation (which is very good) before jumping to implement it, it will make it much easier IMHO.

Also, make sure you have internet permission in your AndroidManifest.xml

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top