Skip to main content

Android remote string reading

2 replies [Last post]
captsisko
Offline
Joined: 2010-07-08
Points: 0

Hi guys,

I am developing an android app and I am attempting to read text and image information from a webpage.
To begin with I am trying to read some plain text with the following code :

<br />
try {<br />
		            URL url = new URL("http://www.test1omniforce.co.uk/sites/all/themes/zen/zen/android/");<br />
		            BufferedReader output;<br />
					try {<br />
						output = new BufferedReader( new InputStreamReader(url.openStream()) );</p>
<p>						Context context = getApplicationContext();<br />
	    				CharSequence text = "Hello toast! "+output;<br />
	    				int duration = Toast.LENGTH_LONG;<br />
	    				Toast toast = Toast.makeText(context, text, duration);toast.show();</p>
<p>					} catch (IOException e) {<br />
						// TODO Auto-generated catch block<br />
						e.printStackTrace();</p>
<p>						Context context = getApplicationContext();<br />
	    				CharSequence text = "failed! ";<br />
	    				int duration = Toast.LENGTH_LONG;<br />
	    				Toast toast = Toast.makeText(context, text, duration);toast.show();<br />
					}<br />

It keeps failing and ending up in the exception area.

Can someone please advise me what I am doing wrong and what I need to do, please ?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
captsisko
Offline
Joined: 2010-07-08
Points: 0

I found out what the nature of the exception is.

It's a [code]java.net.socketexception: permission denied[/code] exception.

Can anyone please shed some light on this for me ?

captsisko
Offline
Joined: 2010-07-08
Points: 0

I found the source of the problem.

With some revision to my code as below :

[code]
try {
URL url = new URL("http://www.mydomain.co.uk/sites/all/themes/zen/zen/");
file = new BufferedReader( new InputStreamReader(url.openStream()) );

String line = "";
while( file.readLine() != null ){
line += file.readLine();
System.out.println("New output : "+line);
}

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
[/code]

I got an exception message in response as follows:

[code]
java.io.IOException: Server returned HTTP response code: 403 for URL: http://www.mydomain.co.uk/sites/all/themes/zen/zen/
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at Sandbox.main(Sandbox.java:21)
[/code]

So, it looks like a permissions issue.

Does anyone know how I can grant my java script access through my domain's directory structure ?