Android Development Network Connection Tool class (1), android tool class

Source: Internet
Author: User

Android Development Network Connection Tool class (1), android tool class

Network Connection Tool:

1 package com.gz civil. utils; 2 3 import java. io. IOException; 4 import java. util. arrayList; 5 import java. util. list; 6 import java. util. map; 7 8 import org. apache. http. httpEntity; 9 import org. apache. http. httpResponse; 10 import org. apache. http. httpStatus; 11 import org. apache. http. nameValuePair; 12 import org. apache. http. client. httpClient; 13 import org. apache. http. client. entity. urlEncodedFormEntit Y; 14 import org. apache. http. client. methods. httpGet; 15 import org. apache. http. client. methods. httpPost; 16 import org. apache. http. impl. client. defaultHttpClient; 17 import org. apache. http. message. basicNameValuePair; 18 import org. apache. http. params. basicHttpParams; 19 import org. apache. http. params. httpConnectionParams; 20 import org. apache. http. params. httpParams; 21 import org. apache. http. util. enti TyUtils; 22 23 import android. content. context; 24 25/** 26 * network connection class 27*28 * @ author LiJinlun 29*30 */31 public class NetUtils {32 33/** 34 * Get Json data 35*36 * @ param 37 * @ return 38 */39 40 public static String post (Map <String, string> paramMap, String URL, Context context) {41 42 String result = null; 43 HttpPost httpRequest = new HttpPost (URL); 44 List <NameValuePair> params = new ArrayList <NameValuePair> (); 45 46 paramMap. put ("machineCode", CommonUtil. getMachineCode (context); 47 paramMap. put ("cid", CommonUtil. getCid (context ). toString (); 48 49 for (Map. entry <String, String> param: paramMap. entrySet () {50 params. add (new BasicNameValuePair (param. getKey (), param. getValue (); 51} 52 // print the log 53 LogUtils. d (SysUtils. LOG_TAG, "package removal:" + URL + params. toString (); 54 HttpEntit Y httpEntity; 55 HttpClient httpClient = null; 56 try {57 httpEntity = new UrlEncodedFormEntity (params, "UTF-8"); 58 httpRequest. setEntity (httpEntity); 59 httpClient = getHttpClient (); 60 HttpResponse httpResponse = httpClient.exe cute (httpRequest); 61 62 int status = httpResponse. getStatusLine (). getStatusCode (); 63 if (status = HttpStatus. SC _ OK) {64 result = EntityUtils. toString (httpResponse. ge TEntity (); 65 // print back the package log 66 LogUtils. d (SysUtils. LOG_TAG, "return package:" + result. toString (); 67 return result; 68} 69} catch (IOException e) {70 e. printStackTrace (); 71 return SysUtils. errorcode. ERROR_TIMEOUT + ""; 72} finally {73 if (httpClient! = Null) 74 httpClient. getConnectionManager (). shutdown (); 75} 76 return null; 77} 78 79/** 80 * get data 81*82 * @ param url 83 * @ return 84 */85 public static String get (String url) {86 // print the unwrapped log 87 LogUtils. d (SysUtils. LOG_TAG, "package removal:" + url. toString (); 88 String result = null; 89 HttpGet get = new HttpGet (url); 90 HttpClient client = new DefaultHttpClient (); 91 try {92 HttpResponse response = client.exe cute (get); 93 int status = response. getStatusLine (). getStatusCode (); 94 if (status = HttpStatus. SC _ OK) {95 result = EntityUtils. toString (response. getEntity (), "UTF-8"); 96 // print back package log 97 LogUtils. d (SysUtils. LOG_TAG, "return package:" + result. toString (); 98 return result; 99} 100} catch (IOException e) {101 e. printStackTrace (); 102 return SysUtils. errorcode. ERROR_TIMEOUT + ""; 103} finally {104 client. getConnectionManager (). shutdown (); 105} 106 return null; 107} 108 109 public static HttpClient getHttpClient () {110 HttpParams httpParams = new BasicHttpParams (); 111 HttpConnectionParams. setConnectionTimeout (httpParams, 8*1000); 112 HttpConnectionParams. setSoTimeout (httpParams, 8*1000); 113 // HttpClientParams. setRedirecting (httpParams, true); 114 HttpClient client = new DefaultHttpClient (httpParams); 115 // client. getParams (). setIntParameter (HttpConnectionParams. SO_TIMEOUT, 116 // 8*1000); // timeout setting 117 // client. getParams (). setIntParameter (118 // HttpConnectionParams. CONNECTION_TIMEOUT, 8*1000); // connection timeout 119 return client; 120} 121 122}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.