0

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:

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;
}
Sujay
  • 6,664
  • 2
  • 27
  • 49
Khafaga
  • 1,347
  • 3
  • 14
  • 22

1 Answers1

0

Are you sure the GPS is sending a line (ended with a new line)?

If not I would use read() repeatedly to get all the data it sends (until an EOF is reached)

user1554966
  • 223
  • 1
  • 2
  • 7