/[projects]/android/TrainInfo/src/dk/thoerup/traininfo/util/DownloadUtil.java
ViewVC logotype

Annotation of /android/TrainInfo/src/dk/thoerup/traininfo/util/DownloadUtil.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 238 - (hide annotations) (download)
Sat Aug 8 20:09:47 2009 UTC (14 years, 10 months ago) by torben
File size: 946 byte(s)
Create a utility class for downloading xml docs, and make fetching of departure data asynchronous
1 torben 238 package dk.thoerup.traininfo.util;
2    
3     import java.io.InputStream;
4     import java.io.InputStreamReader;
5     import java.net.URL;
6     import java.net.URLConnection;
7     import java.util.Date;
8    
9     public class DownloadUtil {
10    
11     public static String getContent(String uri, int timeout, String encoding) throws Exception {
12     char cbuffer[] = new char[256];
13     StringBuffer buffer = new StringBuffer(128000);
14    
15     Date start = new Date();
16     URL url = new URL(uri);
17     URLConnection connection = url.openConnection();
18     connection.setConnectTimeout(20000);
19     InputStream stream = connection.getInputStream();
20    
21     InputStreamReader reader = new InputStreamReader(stream, encoding);
22     int count;
23     while ( (count = reader.read(cbuffer)) != -1) {
24     buffer.append(cbuffer, 0, count);
25    
26     Date now = new Date();
27     if ( (now.getTime()-start.getTime()) > timeout)
28     throw new Exception("timeout");
29     }
30    
31     return buffer.toString();
32     }
33    
34     }

  ViewVC Help
Powered by ViewVC 1.1.20