1

I request a text file from a server. The url does not end in some_file.txt, because the file is created dynamically per the request (not sure if that is even relevant). When I use the code below, I do not read in the text (though I do read in html, if pointed to a url that has html).

            String text = "RESULTS:\n";

            try {  

                String urlString = 
                    "http://appdata.mysite.com/httpauth/" +
                    "hub/DAR_param1_RTQB?" + 
                    "method=query&list=param2"; 

                // Create a URL 
                URL url = new URL(urlString);

                // Read all the text returned by the server
                in = new BufferedReader(new InputStreamReader(url.openStream()));

                String line;
                while ((line = in.readLine()) != null) {
                    text = text + line;
                }                    
            } catch (MalformedURLException e) {
            } catch (IOException e) {
            } catch (Exception e) {
            } finally {
                if ( in != null ) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        Log.e(TAG, "Exception trying to close BufferedReader");
                    }
                }
            } 

            return text;

This returns only "RESULTS:", but none of the text. What am I missing?

Edit: Here is an example of the file. This is displayed in a browser if the url is pasted into the address bar:

20120120_1734
20120120_1725
20120120_1715
20120120_1705
20120120_1655
3
  • Can you post an example of a file this request will generate? Commented Jan 20, 2012 at 17:35
  • @DanW I edited my original post to show the file. Commented Jan 20, 2012 at 17:38
  • @Hap if you only want to display it why now use a WebView? Commented Jan 20, 2012 at 19:06

2 Answers 2

4

You are swallowing all the potential errors that you need to see to troubleshoot this.

In your catch clauses, try adding some code to see the errors. Something along the lines of:

System.out.println("Error: ", e.getMessage());
Sign up to request clarification or add additional context in comments.

1 Comment

OK. Good point. I was getting a File not Found exception. Turns out is was an authentication issue.
0

Did you add the Internet permissions in the manifest? The code looks fine, but you're clearly getting some error and not outputting the errors since you dont have a print message in the catch clause.

Log.i("--->","e.getMessage()); //just like oldingn said 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.