-1

As part of my project I want to read an online text file that contain a url say(www.example.com/download.html).

The text file we can access via (www.example.com/url.txt).

I want to read the content of text file and want to store that in a string. Someone please help me on this. Tried Read text file from server in Android app method but not working.

Community
  • 1
  • 1
HDTV
  • 3
  • 5
  • What do you mean by not working? Is there an error? A crash? Please post the code you have so far. – thegrinner Jun 12 '13 at 12:42
  • Are you getting this exception `NetworkOnMainThreadException` ? Have u added this permission in manifest : `` – The Dark Knight Jun 12 '13 at 12:44
  • It is not reading the text file from the server. Added the permission on manifest. – HDTV Jun 12 '13 at 12:56
  • What errors are you getting ? instead of reading from .txt file try reading using json – onkar Jun 12 '13 at 12:58
  • try { url = new URL("http://www.example.com/update.txt"); BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); urlString = in.readLine(); in.close(); } catch (MalformedURLException e) { } catch (IOException e) { } This is the code for reading... – HDTV Jun 12 '13 at 13:00
  • The entire code http://pastebin.com/vFumNZLA – HDTV Jun 12 '13 at 13:17

2 Answers2

1

Did you check the HttpResponce code you got from the http request to be 200?

Rohan
  • 428
  • 5
  • 10
  • `code` try { url = new URL("http://www.example.com/update.txt"); BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); urlString = in.readLine(); in.close(); } catch (MalformedURLException e) { } catch (IOException e) { } The text file have only one line and that is a url. – HDTV Jun 12 '13 at 13:05
  • I consider using Apache HttpClient class a much standard approad for fetching data over the web. The primary advantage I see is that the responce codes give a lot better idea about what is happening. You can have a bunch of things going on like redirects, access not allowed, certificate related errors. [link](http://developer.android.com/reference/org/apache/http/client/HttpClient.html) – Rohan Jun 12 '13 at 13:17
  • I dont have much idea about java programming. This is a part of my project so I'm just learning from web according to my requirement. If you can give me an example that will be great. Hope you now understand my needs from the whole code. – HDTV Jun 12 '13 at 13:21
  • Sure. This should help you[link](http://stackoverflow.com/questions/6660243/how-do-i-use-httpclient-in-java-to-retrieve-a-binary-file). If you are not able to read still, add the following line before getting the BufferedReader which says: httpResponse.getStatusLine().getStatusCode(). This will tell you the standard response code which you can compare against [link](http://developer.android.com/reference/org/apache/http/HttpStatus.html) – Rohan Jun 12 '13 at 13:30
  • Thanks will test it and let you know... :) – HDTV Jun 12 '13 at 13:51
  • And what does logcat say? You must be seeing an exception log. I see that you are calling this in onCreate() which is called on the main thread. You need to spawn another thread and always do network operations on the background thread. – Rohan Jun 12 '13 at 16:59
  • I got the solution and it is working...have to do that in AsyncTask. – HDTV Jun 12 '13 at 17:07
0

You probably need to create a Thread to handle this request. In most versions Android OS forbids doing network requests in the main thread.

Check this question

Community
  • 1
  • 1
felknight
  • 1,383
  • 1
  • 9
  • 25