/[projects]/android/AndroidUtils/src/dk/thoerup/androidutils/HttpUtil.java
ViewVC logotype

Annotation of /android/AndroidUtils/src/dk/thoerup/androidutils/HttpUtil.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 769 - (hide annotations) (download)
Mon May 31 07:20:27 2010 UTC (14 years ago) by torben
File size: 2751 byte(s)
Import
1 torben 769 package dk.thoerup.androidutils;
2    
3     import java.io.ByteArrayOutputStream;
4     import java.io.IOException;
5     import java.io.InputStream;
6     import java.io.OutputStream;
7     import java.net.HttpURLConnection;
8     import java.net.URL;
9     import java.net.URLConnection;
10     import java.net.URLDecoder;
11     import java.net.URLEncoder;
12     import java.util.HashMap;
13     import java.util.Map;
14    
15    
16     public class HttpUtil {
17    
18     static public Map<String,String> decodeParams(String str) {
19     Map<String,String> res = new HashMap<String,String>();
20    
21    
22     String parts[] = str.split("&");
23    
24     for (String part : parts) {
25     String pair[] = part.split("=");
26     String key = pair[0];
27    
28     String val = null;
29     if (pair.length == 2) {
30     val = URLDecoder.decode(pair[1]);
31     }
32    
33     res.put(key, val);
34     }
35    
36     return res;
37     }
38    
39     static public String encodeParams(Map<String,String> params) {
40     StringBuilder sb = new StringBuilder();
41    
42     boolean isFirst = true;
43     for (String key : params.keySet()) {
44     if (isFirst) {
45     isFirst = false;
46     } else {
47     sb.append("&");
48     }
49    
50     String val = params.get(key);
51     sb.append(key);
52     sb.append("=");
53     sb.append( encode(val) );
54    
55    
56     }
57    
58    
59     return sb.toString();
60    
61     }
62    
63     static String encode(String url) {
64     if (url == null)
65     return "";
66    
67     try {
68     return URLEncoder.encode(url, "UTF-8");
69     } catch (Exception e) {
70     return url;
71     }
72     }
73    
74    
75     public static byte[] getContent(String uri, int timeout) throws IOException {
76     URL url = new URL(uri);
77     URLConnection connection = url.openConnection();
78    
79     connection.setConnectTimeout(timeout);
80     InputStream is = connection.getInputStream();
81    
82     return readInputStream(is);
83     }
84    
85     public static byte[] postContent(String uri, String data, int timeout) throws IOException {
86     URL url = new URL(uri);
87     HttpURLConnection connection = (HttpURLConnection) url.openConnection();
88     connection.setDoOutput(true);
89     connection.setRequestMethod("POST");
90     connection.setConnectTimeout(timeout);
91    
92     OutputStream os = connection.getOutputStream();
93     os.write( data.getBytes() );
94    
95     InputStream is = connection.getInputStream();
96    
97     return readInputStream(is);
98    
99     }
100    
101     static byte[] readInputStream(InputStream is) throws IOException{
102     byte buffer[] = new byte[1024];
103     ByteArrayOutputStream baos = new ByteArrayOutputStream(32768); //start buffer size - instead of default 32bytes
104     try {
105     int count;
106     while ( (count = is.read(buffer)) != -1) {
107     baos.write(buffer, 0, count);
108     }
109     } finally {
110     try {
111     is.close();
112     baos.close();
113     } catch (Exception e) {} //never mind if close throws an exception
114     }
115    
116     return baos.toByteArray();
117     }
118    
119     }

  ViewVC Help
Powered by ViewVC 1.1.20