HttpClient applications in Android (POST method)

Source: Internet
Author: User

First download the HttpClient package in http://hc.apache.org/downloads.cgi

Look directly at the code

1 ImportAndroid.os.Handler;2 ImportAndroid.os.Message;3 Importandroid.support.v7.app.AppCompatActivity;4 ImportAndroid.os.Bundle;5 ImportAndroid.util.Log;6 ImportAndroid.view.View;7 ImportAndroid.widget.TextView;8 9 ImportCom.google.gson.Gson;Ten ImportCom.google.gson.reflect.TypeToken; One  A Importorg.apache.http.HttpEntity; - ImportOrg.apache.http.HttpResponse; - ImportOrg.apache.http.HttpStatus; the ImportOrg.apache.http.NameValuePair; - Importorg.apache.http.client.ClientProtocolException; - Importorg.apache.http.client.HttpClient; - Importorg.apache.http.client.entity.UrlEncodedFormEntity; + ImportOrg.apache.http.client.methods.HttpGet; - ImportOrg.apache.http.client.methods.HttpPost; + Importorg.apache.http.client.utils.URLEncodedUtils; A Importorg.apache.http.impl.client.DefaultHttpClient; at ImportOrg.apache.http.message.BasicNameValuePair; - ImportOrg.apache.http.protocol.HTTP; - Importorg.apache.http.util.EntityUtils; -  - Importjava.io.IOException; - Importjava.io.UnsupportedEncodingException; in ImportJava.lang.reflect.Type; - Importjava.util.ArrayList; to Importjava.util.LinkedList; + Importjava.util.List; -  the  Public classMainactivityextendsappcompatactivity { *  $ @OverridePanax Notoginseng     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); the Setcontentview (r.layout.activity_main); +         //Open a sub-thread for network operation, wait for return result, use handler to notify UI A         NewThread (Networktask). Start (); the  +     } -  $Handler Handler =NewHandler () { $ @Override -          Public voidhandlemessage (Message msg) { -             Super. Handlemessage (msg); theBundle data =Msg.getdata (); -String val = data.getstring ("Value");WuyiLOG.I ("MyLog", "request result--" +val); the             //TODO -             //updates to the UI interface and other related actions Wu         } -     }; About  $     /** - * Network operation related sub-threads -      */ -Runnable Networktask =NewRunnable () { A @Override +          Public voidrun () { the             //TODO -             //HTTP request is made here. Network requests related Operations $String TAG = "ABC"; the  the             //BASEURL theString url = "http://192.168.8.7/insert.php"; the  -HttpPost request =Newhttppost (URL); in  the             //first put the parameters in the list, and then URL code the parameters thelist<basicnamevaluepair> params =NewLinkedlist<basicnamevaluepair>(); AboutParams.add (NewBasicnamevaluepair ("FirstName", "This is Android")); theParams.add (NewBasicnamevaluepair ("LastName", "This is Androga in.")); theParams.add (NewBasicnamevaluepair ("Age", "66")); the  +             Try { -httpentity entity =NewUrlencodedformentity (params, "UTF-8"); the Bayi request.setentity (entity); the  theHttpClient client =Newdefaulthttpclient (); -  -HttpResponse response =Client.execute (request); the  theLOG.E (TAG, "Response Code:" + the response.getstatusline (). Getstatuscode ()); the  -                 if(Response.getstatusline (). Getstatuscode () = =HTTPSTATUS.SC_OK) { the  theString data =entityutils.tostring (Response.getentity ()); the 94                 } the}Catch(IOException e) { theLOG.E (TAG, "~~~~~~" +e); the e.printstacktrace ();98             } AboutMessage msg =NewMessage (); -Bundle data =NewBundle ();101Data.putstring ("value", "Request Result");102 msg.setdata (data);103 handler.sendmessage (msg);104         } the     }106}

It is important to note that HTTP requests cannot be placed in Maintread after Android 3.0.

HttpClient applications in Android (POST method)

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.