Android Client socket , how to read data?

后端 未结 2 1321
执念已碎
执念已碎 2020-12-30 13:53

here\'s my full code: the cnx is established , and i am sending data to server , but i cant read anything from the server...

public class client extends Acti         


        
2条回答
  •  南笙
    南笙 (楼主)
    2020-12-30 14:19

    HttpResponse response = m_httpClient.execute( request );
    String result = "";
    if( response.getStatusLine().getStatusCode() == HttpStatus.SC_OK )
    {
    // open stream
          InputStream stream = response.getEntity().getContent();
    
          if( stream != null )
          {
            int len = 0;
            byte[] buf = new byte[ 1024 ];
    
            try
            {
              ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    
              while( ( len = stream.read( buf ) ) > 0 )
              {
                outputStream.write( buf, 0, len );
              }
    
              buf = outputStream.toByteArray();
              result = EncodingUtils.getAsciiString( buf );
            }
            catch( IOException e )
            {
              e.printStackTrace();
            }
            finally
            {
              stream.close();
            }
    }
    

提交回复
热议问题