-1

already asked in another post, but got no solution see me script bellow, how can i change that, please full demo, so that i can set a readtimout if the network is down for example

at all i have to connect to an url, read just one line, get that as a string ... done hope u can give an example. any other working way is welcome.. thx

try {
URL url = new URL("http://mydomain/myfile.php");

//url.setReadTimeout(5000); does not work

InputStreamReader testi= new InputStreamReader(url.openStream());
BufferedReader in = new BufferedReader(testi);

    //in.setReadTimeout(5000); does not work

stri = in.readLine();   
Log.v ("GotThat: ",stri);
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
1
  • What is the problem you're having? What are you trying to do, exactly? Commented Sep 12, 2013 at 19:28

2 Answers 2

0

How about using HttpURLConnection

import java.net.HttpURLConnection;

   URL url = new URL("http://mydomain/myfile.php");

   HttpURLConnection huc = (HttpURLConnection) url.openConnection();
   HttpURLConnection.setFollowRedirects(false);
   huc.setConnectTimeout(15 * 1000);
   huc.setRequestMethod("GET");
   huc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)");
   huc.connect();
   InputStream input = huc.getInputStream();

Code picked from here

Sign up to request clarification or add additional context in comments.

Comments

0

Timeout:

URL url = new URL("http://www.google.com");
URLConnection conn = url.openConnection();
conn.connect();
conn.setReadTimeout(10000); //10000 milliseconds (10 seconds)

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.