Android 网络连接工具类封装,支持wifi以及3G网络

市场上几乎每款APP都具有网络连接的功能。而对于开发项目来说,连接网络也是经常会用到的。现在麦洛将网络连接的代码封装成工具类,
这些代码在写与服务器交互的应用时,是非常有用的。开发的同学可以直接拿来使用。而对于麦洛自己也是一个知识积累的过程,何乐而不为呢?

在写网络连接代码之前,先了解一下HTTP协议。HTTP协议概念不用多解释了,无非就是基于TCP协议的一个应用协议。客户端对服务端发送请求,
服务端对客户端的请求作出响应。
Android中对网络连接的支持是非常优秀的。除了java.net.*包和apache包中的API,Android本身也对网络连接进行了封装,体现在android.net.*包中。
所以在Android中实现网络连接是非常容易的。

本文的实现涉及到三个类,Utility.java,RequestParameters.java,RequestException.java。Utility类就是封装网络连接的最主要接口,而RequestParameters封装了请求
数据,而RequestException是封装了请求error时的数据。
其实,这几个类是麦洛在写新浪微博时,从新浪微博中抽取出来的。麦洛觉得好的东西应该不断积累,然后在实际项目中熟悉和运用,最后形成自己独特的知识。
而现在就是积累的第一步。

首先看Utility类的实现,这个类的封装其实也不难,看看新浪微博中的实现,学习一下吧

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/**
 * Utility class for http request.
 * 
 */
 
public class Utility {
 
    private static RequestParameters mRequestHeader = new RequestParameters();
 
    public static final String BOUNDARY = "7cd4a6d158c";
    public static final String MP_BOUNDARY = "--" + BOUNDARY;
    public static final String END_MP_BOUNDARY = "--" + BOUNDARY + "--";
    public static final String MULTIPART_FORM_DATA = "multipart/form-data";
 
    public static final String HTTPMETHOD_POST = "POST";
    public static final String HTTPMETHOD_GET = "GET";
    public static final String HTTPMETHOD_DELETE = "DELETE";
 
    private static final int SET_CONNECTION_TIMEOUT = 50000;
    private static final int SET_SOCKET_TIMEOUT = 200000;
 
    // 设置http头,如果authParam不为空,则表示当前有token认证信息需要加入到头中
    public static void setHeader(String httpMethod, HttpUriRequest request,
            RequestParameters authParam, String url) throws RequestException {
        if (!isBundleEmpty(mRequestHeader)) {
            for (int loc = 0; loc < mRequestHeader.size(); loc++) {
                String key = mRequestHeader.getKey(loc);
                request.setHeader(key, mRequestHeader.getValue(key));
            }
        }
        if (!isBundleEmpty(authParam)) {
            String authHeader = "";
            if (authHeader != null) {
                request.setHeader("Authorization", authHeader);
            }
        }
        request.setHeader("User-Agent", System.getProperties().getProperty("http.agent")
                + " WeiboAndroidSDK");
    }
 
    public static boolean isBundleEmpty(RequestParameters bundle) {
        if (bundle == null || bundle.size() == 0) {
            return true;
        }
        return false;
    }
 
    // 填充request bundle
    public static void setRequestHeader(String key, String value) {
        // mRequestHeader.clear();
        mRequestHeader.add(key, value);
    }
 
    public static void setRequestHeader(RequestParameters params) {
        mRequestHeader.addAll(params);
    }
 
    public static void clearRequestHeader() {
        mRequestHeader.clear();
 
    }
 
    public static String encodePostBody(Bundle parameters, String boundary) {
        if (parameters == null)
            return "";
        StringBuilder sb = new StringBuilder();
 
        for (String key : parameters.keySet()) {
            if (parameters.getByteArray(key) != null) {
                continue;
            }
 
            sb.append("Content-Disposition: form-data; name=\"" + key + "\"\r\n\r\n"
                    + parameters.getString(key));
            sb.append("\r\n" + "--" + boundary + "\r\n");
        }
 
        return sb.toString();
    }
 
    public static String encodeUrl(RequestParameters parameters) {
        if (parameters == null) {
            return "";
        }
 
        StringBuilder sb = new StringBuilder();
        boolean first = true;
        for (int loc = 0; loc < parameters.size(); loc++) {
            if (first)
                first = false;
            else
                sb.append("&");
            sb.append(URLEncoder.encode(parameters.getKey(loc)) + "="
                    + URLEncoder.encode(parameters.getValue(loc)));
        }
        return sb.toString();
    }
 
    public static Bundle decodeUrl(String s) {
        Bundle params = new Bundle();
        if (s != null) {
            String array[] = s.split("&");
            for (String parameter : array) {
                String v[] = parameter.split("=");
                params.putString(URLDecoder.decode(v[0]), URLDecoder.decode(v[1]));
            }
        }
        return params;
    }
 
    /**
     * Parse a URL query and fragment parameters into a key-value bundle.
     * 
     * @param url
     *            the URL to parse
     * @return a dictionary bundle of keys and values
     */
    public static Bundle parseUrl(String url) {
        // hack to prevent MalformedURLException
        url = url.replace("weiboconnect", "http");
        try {
            URL u = new URL(url);
            Bundle b = decodeUrl(u.getQuery());
            b.putAll(decodeUrl(u.getRef()));
            return b;
        } catch (MalformedURLException e) {
            return new Bundle();
        }
    }
 
    /**
     * Construct a url encoded entity by parameters .
     * 
     * @param bundle
     *            :parameters key pairs
     * @return UrlEncodedFormEntity: encoed entity
     */
    public static UrlEncodedFormEntity getPostParamters(Bundle bundle) throws RequestException {
        if (bundle == null || bundle.isEmpty()) {
            return null;
        }
        try {
            List form = new ArrayList();
            for (String key : bundle.keySet()) {
                form.add(new BasicNameValuePair(key, bundle.getString(key)));
            }
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(form, "UTF-8");
            return entity;
        } catch (UnsupportedEncodingException e) {
            throw new RequestException(e);
        }
    }
 
    /**
     * Implement a weibo http request and return results .
     * 
     * @param context
     *            : context of activity
     * @param url
     *            : request url of open api
     * @param method
     *            : HTTP METHOD.GET, POST, DELETE
     * @param params
     *            : Http params , query or postparameters
     * @return UrlEncodedFormEntity: encoed entity
     */
 
    public static String openUrl(Context context, String url, String method,
            RequestParameters params) throws RequestException {
        String rlt = "";
        String file = "";
        if(params!=null){
        	for (int loc = 0; loc < params.size(); loc++) {         		String key = params.getKey(loc);         		if (key.equals("pic")) {         			file = params.getValue(key);         			params.remove(key);         		}         	}         }         if (TextUtils.isEmpty(file)) {             rlt = openUrl(context, url, method, params, null);         } else {             rlt = openUrl(context, url, method, params, file);         }         return rlt;     }     public static String openUrl(Context context, String url, String method,     		RequestParameters params, String file) throws RequestException {         String result = "";         try {             HttpClient client = getNewHttpClient(context);             HttpUriRequest request = null;             ByteArrayOutputStream bos = null;             if (method.equals("GET")) {                 url = url + "?" + encodeUrl(params);                 HttpGet get = new HttpGet(url);                 request = get;             } else if (method.equals("POST")) {                 HttpPost post = new HttpPost(url);                 byte[] data = null;                 bos = new ByteArrayOutputStream(1024 * 50);                 if (!TextUtils.isEmpty(file)) {                     Utility.paramToUpload(bos, params);                     post.setHeader("Content-Type", MULTIPART_FORM_DATA + "; boundary=" + BOUNDARY);                     Bitmap bf = BitmapFactory.decodeFile(file);                     Utility.imageContentToUpload(bos, bf);                 } else {                     post.setHeader("Content-Type", "application/x-www-form-urlencoded");                     String postParam = encodeParameters(params);                     data = postParam.getBytes("UTF-8");                     bos.write(data);                 }                 data = bos.toByteArray();                 bos.close();                 // UrlEncodedFormEntity entity = getPostParamters(params);                 ByteArrayEntity formEntity = new ByteArrayEntity(data);                 post.setEntity(formEntity);                 request = post;             } else if (method.equals("DELETE")) {                 request = new HttpDelete(url);             }             setHeader(method, request, params, url);             HttpResponse response = client.execute(request);             StatusLine status = response.getStatusLine();             int statusCode = status.getStatusCode();             if (statusCode != 200) {                 result = read(response);                 throw new RequestException(String.format(status.toString()), statusCode);             }             // parse content stream from response             result = read(response);             return result;         } catch (IOException e) {             throw new RequestException(e);         }     }     public static HttpClient getNewHttpClient(Context context) {         try {             KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());             trustStore.load(null, null);             SSLSocketFactory sf = new MySSLSocketFactory(trustStore);             sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);             HttpParams params = new BasicHttpParams();             HttpConnectionParams.setConnectionTimeout(params, 10000);             HttpConnectionParams.setSoTimeout(params, 10000);             HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);             HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);             SchemeRegistry registry = new SchemeRegistry();             registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));             registry.register(new Scheme("https", sf, 443));             ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);             // Set the default socket timeout (SO_TIMEOUT) // in             // milliseconds which is the timeout for waiting for data.             HttpConnectionParams.setConnectionTimeout(params, Utility.SET_CONNECTION_TIMEOUT);             HttpConnectionParams.setSoTimeout(params, Utility.SET_SOCKET_TIMEOUT);             HttpClient client = new DefaultHttpClient(ccm, params);             WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);             if (!wifiManager.isWifiEnabled()) {                 // 获取当前正在使用的APN接入点                 Uri uri = Uri.parse("content://telephony/carriers/preferapn");                 Cursor mCursor = context.getContentResolver().query(uri, null, null, null, null);                 if (mCursor != null && mCursor.moveToFirst()) {                     // 游标移至第一条记录,当然也只有一条                     String proxyStr = mCursor.getString(mCursor.getColumnIndex("proxy"));                     if (proxyStr != null && proxyStr.trim().length() > 0) {
                        HttpHost proxy = new HttpHost(proxyStr, 80);
                        client.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);
                    }
                    mCursor.close();
                }
            }
            return client;
        } catch (Exception e) {
            return new DefaultHttpClient();
        }
    }
 
    public static class MySSLSocketFactory extends SSLSocketFactory {
        SSLContext sslContext = SSLContext.getInstance("TLS");
 
        public MySSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException,
                KeyManagementException, KeyStoreException, UnrecoverableKeyException {
            super(truststore);
 
            TrustManager tm = new X509TrustManager() {
                public void checkClientTrusted(X509Certificate[] chain, String authType)
                        throws CertificateException {
                }
 
                public void checkServerTrusted(X509Certificate[] chain, String authType)
                        throws CertificateException {
                }
 
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            };
 
            sslContext.init(null, new TrustManager[] { tm }, null);
        }
 
        @Override
        public Socket createSocket(Socket socket, String host, int port, boolean autoClose)
                throws IOException, UnknownHostException {
            return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
        }
 
        @Override
        public Socket createSocket() throws IOException {
            return sslContext.getSocketFactory().createSocket();
        }
    }
 
    /**
     * Get a HttpClient object which is setting correctly .
     * 
     * @param context
     *            : context of activity
     * @return HttpClient: HttpClient object
     */
    public static HttpClient getHttpClient(Context context) {
        BasicHttpParams httpParameters = new BasicHttpParams();
        // Set the default socket timeout (SO_TIMEOUT) // in
        // milliseconds which is the timeout for waiting for data.
        HttpConnectionParams.setConnectionTimeout(httpParameters, Utility.SET_CONNECTION_TIMEOUT);
        HttpConnectionParams.setSoTimeout(httpParameters, Utility.SET_SOCKET_TIMEOUT);
        HttpClient client = new DefaultHttpClient(httpParameters);
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        if (!wifiManager.isWifiEnabled()) {
            // 获取当前正在使用的APN接入点
            Uri uri = Uri.parse("content://telephony/carriers/preferapn");
            Cursor mCursor = context.getContentResolver().query(uri, null, null, null, null);
            if (mCursor != null && mCursor.moveToFirst()) {
                // 游标移至第一条记录,当然也只有一条
                String proxyStr = mCursor.getString(mCursor.getColumnIndex("proxy"));
                if (proxyStr != null && proxyStr.trim().length() > 0) {
                    HttpHost proxy = new HttpHost(proxyStr, 80);
                    client.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);
                }
                mCursor.close();
            }
        }
        return client;
    }
 
    /**
     * Upload image into output stream .
     * 
     * @param out
     *            : output stream for uploading weibo
     * @param imgpath
     *            : bitmap for uploading
     * @return void
     */
    private static void imageContentToUpload(OutputStream out, Bitmap imgpath)
            throws RequestException {
        StringBuilder temp = new StringBuilder();
 
        temp.append(MP_BOUNDARY).append("\r\n");
        temp.append("Content-Disposition: form-data; name=\"pic\"; filename=\"")
                .append("news_image").append("\"\r\n");
        String filetype = "image/png";
        temp.append("Content-Type: ").append(filetype).append("\r\n\r\n");
        byte[] res = temp.toString().getBytes();
        BufferedInputStream bis = null;
        try {
            out.write(res);
            imgpath.compress(CompressFormat.PNG, 75, out);
            out.write("\r\n".getBytes());
            out.write(("\r\n" + END_MP_BOUNDARY).getBytes());
        } catch (IOException e) {
            throw new RequestException(e);
        } finally {
            if (null != bis) {
                try {
                    bis.close();
                } catch (IOException e) {
                    throw new RequestException(e);
                }
            }
        }
    }
 
    /**
     * Upload weibo contents into output stream .
     * 
     * @param baos
     *            : output stream for uploading weibo
     * @param params
     *            : post parameters for uploading
     * @return void
     */
    private static void paramToUpload(OutputStream baos, RequestParameters params)
            throws RequestException {
        String key = "";
        for (int loc = 0; loc < params.size(); loc++) {             key = params.getKey(loc);             StringBuilder temp = new StringBuilder(10);             temp.setLength(0);             temp.append(MP_BOUNDARY).append("\r\n");             temp.append("content-disposition: form-data; name=\"").append(key).append("\"\r\n\r\n");             temp.append(params.getValue(key)).append("\r\n");             byte[] res = temp.toString().getBytes();             try {                 baos.write(res);             } catch (IOException e) {                 throw new RequestException(e);             }         }     }     /**      * Read http requests result from response .      *       * @param response      *            : http response by executing httpclient      *       * @return String : http response content      */     private static String read(HttpResponse response) throws RequestException {         String result = "";         HttpEntity entity = response.getEntity();         InputStream inputStream;         try {             inputStream = entity.getContent();             ByteArrayOutputStream content = new ByteArrayOutputStream();             Header header = response.getFirstHeader("Content-Encoding");             if (header != null && header.getValue().toLowerCase().indexOf("gzip") > -1) {
                inputStream = new GZIPInputStream(inputStream);
            }
 
            // Read response into a buffered stream
            int readBytes = 0;
            byte[] sBuffer = new byte[512];
            while ((readBytes = inputStream.read(sBuffer)) != -1) {
                content.write(sBuffer, 0, readBytes);
            }
            // Return result from buffered stream
            result = new String(content.toByteArray());
            return result;
        } catch (IllegalStateException e) {
            throw new RequestException(e);
        } catch (IOException e) {
            throw new RequestException(e);
        }
    }
 
    /**
     * Read http requests result from inputstream .
     * 
     * @param inputstream
     *            : http inputstream from HttpConnection
     * 
     * @return String : http response content
     */
    private static String read(InputStream in) throws IOException {
        StringBuilder sb = new StringBuilder();
        BufferedReader r = new BufferedReader(new InputStreamReader(in), 1000);
        for (String line = r.readLine(); line != null; line = r.readLine()) {
            sb.append(line);
        }
        in.close();
        return sb.toString();
    }
 
    /**
     * Clear current context cookies .
     * 
     * @param context
     *            : current activity context.
     * 
     * @return void
     */
    public static void clearCookies(Context context) {
        @SuppressWarnings("unused")
        CookieSyncManager cookieSyncMngr = CookieSyncManager.createInstance(context);
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.removeAllCookie();
    }
 
    /**
     * Display a simple alert dialog with the given text and title.
     * 
     * @param context
     *            Android context in which the dialog should be displayed
     * @param title
     *            Alert dialog title
     * @param text
     *            Alert dialog message
     */
    public static void showAlert(Context context, String title, String text) {
        Builder alertBuilder = new Builder(context);
        alertBuilder.setTitle(title);
        alertBuilder.setMessage(text);
        alertBuilder.create().show();
    }
 
    public static String encodeParameters(RequestParameters httpParams) {
        if (null == httpParams || Utility.isBundleEmpty(httpParams)) {
            return "";
        }
        StringBuilder buf = new StringBuilder();
        int j = 0;
        for (int loc = 0; loc < httpParams.size(); loc++) {
            String key = httpParams.getKey(loc);
            if (j != 0) {
                buf.append("&");
            }
            try {
                buf.append(URLEncoder.encode(key, "UTF-8")).append("=")
                        .append(URLEncoder.encode(httpParams.getValue(key), "UTF-8"));
            } catch (java.io.UnsupportedEncodingException neverHappen) {
            }
            j++;
        }
        return buf.toString();
 
    }
 
    /**
     * Base64 encode mehtod for weibo request.Refer to weibo development
     * document.
     * 
     */
    public static char[] base64Encode(byte[] data) {
        final char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
                .toCharArray();
        char[] out = new char[((data.length + 2) / 3) * 4];
        for (int i = 0, index = 0; i < data.length; i += 3, index += 4) {
            boolean quad = false;
            boolean trip = false;
            int val = (0xFF & (int) data[i]);
            val <>= 6;
            out[index + 2] = alphabet[(trip ? (val & 0x3F) : 64)];
            val >>= 6;
            out[index + 1] = alphabet[val & 0x3F];
            val >>= 6;
            out[index + 0] = alphabet[val & 0x3F];
        }
        return out;
    }
 
}

另外两个类比较简单,可以到googlecode中下载完整的项目来查看

http://code.google.com/p/http-wrap/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值