如何使用套接字连接阅读内容?

时间:2011-11-16 06:37:04

标签: android

任何人都可以帮助使用套接字连接阅读内容吗?

我使用了以下代码行:

 ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
 String message = (String) ois.readObject();
 System.out.println("Message Received: " + message);

此致 Monali

2 个答案:

答案 0 :(得分:0)

试试这段代码:

private static void trustAllHosts()
{
    // Create a trust manager that does not validate certificate chains
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager()
    {
        public java.security.cert.X509Certificate[] getAcceptedIssuers()
        {
            return new java.security.cert.X509Certificate[] {};
        }

        public void checkClientTrusted(X509Certificate[] chain,
                String authType) throws CertificateException
        {}

        public void checkServerTrusted(X509Certificate[] chain,
                String authType) throws CertificateException
        {}
    }

    // Install the all-trusting trust manager
    try
    {
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection
        .setDefaultSSLSocketFactory(sc.getSocketFactory());
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

答案 1 :(得分:0)

尝试以下代码段

BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
    total.append(line);
}