简体   繁体   English

java.net.SocketException:从流读取时重置连接

[英]java.net.SocketException: Connection reset while reading from a stream

I've developed some code that can access a url and reads a stream from it , but when I'm trying to get the stream I get java.net.SocketException .Here is the stack trace: 我已经开发了一些可以访问URL并从中读取流的代码,但是当我尝试获取流时,我得到了java.net.SocketException。这是堆栈跟踪:

java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:168)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
    at java.io.BufferedInputStream.read1(BufferedInputStream.java:258)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:317)
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:687)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:632)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:652)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1049)
    at utils.AljazemArabicWordsGrabber.grab(AljazemArabicWordsGrabber.java:46)
    at utils.TranslatorThread.run(TranslatorThread.java:39)

and here is the code that causes the exception: 这是导致异常的代码:

public String[] grab(EnglishWord englishWord) {

    try {
        aljazemURL = new URL(urlLink + englishWord.getLemma());
        connection = aljazemURL.openConnection();

        //connection.connect();
    //    System.out.println("connection:" + connection);
      ////  stream = connection.getInputStream();


    //    reader = new InputStreamReader(stream);
        in = new BufferedReader(new InputStreamReader(connection.getInputStream())); // the exception occurs here..


        while ((decodedString = in.readLine()) != null) {
            if (decodedString.contains("<div class=\"default_to_trans_ar\" style=\"display:block\">")) {
                decodedString = decodedString.replace("<div class=\"default_to_trans_ar\" style=\"display:block\">", "");
                decodedString = decodedString.replace("</div>", "");
                slicedWords = decodedString.split(",");
                for (String slice : slicedWords) {
                    System.out.println(slice);
                }
                //  System.out.println(decodedString);
            }

        }

        in.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

    return slicedWords;
}

Are you sure the GPS is sending a line (ended with a new line)? 您确定GPS正在发送线路(以新线路结尾)吗?

If not I would use read() repeatedly to get all the data it sends (until an EOF is reached) 如果没有,我会反复使用read()来获取它发送的所有数据(直到到达EOF为止)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM