4

Possible Duplicate:
Socket gets stuck when doing readLine()

I wrote jar library and want to use it in my android project. Here one important method from this library:

private static String convertStreamToString(InputStream is) {
        /* is - it's the inputStream created from server response entity. 
        I use org.apache.http classes to communicate with server. */
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) { // <-- this line
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

If I use my library to build java applets and run it on a PC - this code works greate.

But if I use it in Android application and run it on emulator or device, it can stucks sometimes (not always!) for a very long time on a marked line. So SockedTimeoutExceptions happens in this case.

So, I would like to know the reason. And it would be greate if you will help me to get rid from this problem.

Community
  • 1
  • 1
Evgeny
  • 1,413
  • 2
  • 12
  • 16
  • See this post: http://stackoverflow.com/questions/7927855/how-to-read-line-by-line-in-android and this: http://stackoverflow.com/questions/7757544/socket-gets-stuck-when-doing-readline – Christophe Roussy Jan 24 '13 at 16:10
  • Christophe, first link is defently not related to my problem. The second one does't have a solution. – Evgeny Jan 24 '13 at 16:48
  • Also, the main point is that my method for the same data set works differently on the android device and on the PC. It means that the problem is not in the server response format. – Evgeny Jan 24 '13 at 16:54
  • readLine is a blocking method, so if nothing is being received, it will wait blocking execution right there. – Jambaaz Jan 24 '13 at 17:30

0 Answers0