Android socket receive data

18,551
class Client extends Thread {

    public void run()
    {
         try
         {
              Socket socket = new Socket(address, PORT);
              PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
              out.println(json);
              BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
              String response = in.readLine()
              Toast.makeText(MainActivity.this, response, Toast.LENGTH_SHORT).show();
              socket.close();
         }
         catch(Exception e) {}
    }
}
Share:
18,551
gematzab
Author by

gematzab

Rails Prototyping. Start Up for theme theme tourism

Updated on June 04, 2022

Comments

  • gematzab
    gematzab almost 2 years

    I am new with android development so if any further information needed plz ask... I am trying to use sockets to send coordinates to server, receive query results and "Toast" them. Until now in my activity:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_socket);
    
        new Thread(new ClientThread()).start();     
    
    }
          ...
    
     class ClientThread implements Runnable {
                 @SuppressWarnings("deprecation")
                @Override
                 public void run() {
                     try {
                         InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
                         socket = new Socket(serverAddr, SERVERPORT);
    
                     } catch (UnknownHostException e1) {
                         e1.printStackTrace();
                     } catch (IOException e1) {
                         e1.printStackTrace();
                     }
                 }
             }
    
    
          public void onClickk(View view) {
                 try {
                    // Acquire a reference to the system Location Manager
                    LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);............here some more code regarding coordinates. ............
    

    And I am writing through the socket the coordinates (in the same method):

                            lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    
                            if (lat!=null){
    
                                String json = "";
    
                                // 3. build jsonObject
                                JSONObject jsonObject = new JSONObject();
                                try {
                                    jsonObject.accumulate("lat", lat);
                                    jsonObject.accumulate("lon", lon);
                                } catch (JSONException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                                // 4. convert JSONObject to JSON to String
                                json = jsonObject.toString();
    
                             PrintWriter out = new PrintWriter(new BufferedWriter(
                                     new OutputStreamWriter(socket.getOutputStream())),
                                     true);
                             out.println(json);
    

    How I can receive data sent from the server? I am not sure how to use the InputSteam.

    UPDATE: My server. WHen a message is received from the app the server tries to write a string on the socket.

           sock.on('data', function(data) {
    
              console.log('DATA ' + sock.remoteAddress + ': ' + data);
    
              sock.write(la);
           });
    
           });