Android Http request framework 1: Get and Post requests, androidget

Source: Internet
Author: User

Android Http request framework 1: Get and Post requests, androidget

1. HttpUtil

Package com. app. android01; import java. io. bufferedReader; import java. io. IOException; import java. io. inputStream; import java. io. inputStreamReader; import java.net. httpURLConnection; import java.net. URL; import java. util. arrayList; import java. util. list; import org. apache. http. httpResponse; import org. apache. http. nameValuePair; import org. apache. http. client. clientProtocolException; import org. apache. http. clien T. httpClient; import org. apache. http. client. entity. urlEncodedFormEntity; import org. apache. http. client. methods. httpPost; import org. apache. http. impl. client. defaultHttpClient; import org. apache. http. message. basicNameValuePair; import org. apache. http. params. coreConnectionPNames; import org. apache. http. protocol. HTTP; import org. apache. http. util. entityUtils; public class HttpUtil {/*** get request * @ param httpUr L * @ return * @ throws */public String httpGet (String httpUrl) {String result = ""; try {BufferedReader reader = null; StringBuffer sbf = new StringBuffer (); URL url = new URL (httpUrl); HttpURLConnection connection = (HttpURLConnection) url. openConnection (); // set the timeout value to 10 s. setConnectTimeout (10000); // sets the request method connection. setRequestMethod ("GET"); connection. connect (); InputStream Is = connection. getInputStream (); reader = new BufferedReader (new InputStreamReader (is, "UTF-8"); String strRead = null; while (strRead = reader. readLine ())! = Null) {sbf. append (strRead); sbf. append ("\ r \ n");} reader. close (); result = sbf. toString ();} catch (Exception e) {e. printStackTrace ();} return result;}/*** httpPost Request * @ param httpUrl * @ return */public String httpPost (String httpUrl) {String result = ""; // Step 1: Create an HttpPost object HttpPost httpPost = new HttpPost (httpUrl ); // set the http post request parameters using the NameValuePair Object List <NameValuePair> params = new ArrayList <NameValuePair> (); params. add (new BasicNameValuePair ("action", "downloadAndroidApp"); params. add (new BasicNameValuePair ("packageId", "89dcb664-50a7-4bf2-aeed-49c08af6a58a"); params. add (new BasicNameValuePair ("uuid", "test_ok1"); HttpResponse httpResponse = null; try {// set the httpPost request parameter httpPost. setEntity (new UrlEncodedFormEntity (params, HTTP. UTF_8); HttpClient httpClient = new DefaultHttpClient (); // The request times out for 10 s httpClient. getParams (). setParameter (CoreConnectionPNames. CONNECTION_TIMEOUT, 10000); // read timeout 10 s httpClient. getParams (). setParameter (CoreConnectionPNames. SO_TIMEOUT, 10000); httpResponse = httpClient.exe cute (httpPost); if (httpResponse. getStatusLine (). getStatusCode () = 200) {// step 3, use the getEntity method to return the result = EntityUtils. toString (httpResponse. getEntity () ;}} catch (ClientProtocolException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} return result ;}}

 

2. asynchronous requests

1/** 2 * asynchronous request 3 * @ author admin 4 */5 class GetData extends AsyncTask <String, Integer, string> {6 7 @ Override 8 protected String doInBackground (String... params) {9 HttpUtil httpUtil = new HttpUtil (); 10 String resutl = httpUtil. httpGet (params [0]); 11 if (resutl = null) {12 return ""; 13} 14 return resutl; 15} 16 17 @ Override18 protected void onPostExecute (String result) {19 super. onPostExecute (result); 20} 21}

 

 

Android Http request framework 2: network requests of the xUtils framework

 

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.