-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) {
}
christian Muller
  • 5,016
  • 6
  • 34
  • 44

2 Answers2

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

Community
  • 1
  • 1
prijupaul
  • 2,076
  • 2
  • 15
  • 17
0

Timeout:

URL url = new URL("http://www.google.com");
URLConnection conn = url.openConnection();
conn.connect();
conn.setReadTimeout(10000); //10000 milliseconds (10 seconds)
Eduardo
  • 6,900
  • 17
  • 77
  • 121