Eighth chapter, Post network request demo (Android)

Source: Internet
Author: User

Add Network permissions in Androidmanifest.xml

<uses-permission android:name= "Android.permission.INTERNET"/>  
Activity_main.xml

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android:orientation= "vertical"    tools:context= ". Mainactivity ">    <button        android:id=" @+id/main_button "        android:layout_width=" Match_parent "        android:layout_height= "wrap_content"        android:text= "POST request"/>    <textview        android:id= "@+ Id/main_text "        android:layout_width=" match_parent "        android:layout_height=" Wrap_content "/></ Linearlayout>
Mainactivity.java

Package Com.example.demo0616;import Java.util.arraylist;import Java.util.list;import org.apache.http.HttpEntity; Import Org.apache.http.httpresponse;import Org.apache.http.namevaluepair;import org.apache.http.client.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.protocol.http;import Org.apache.http.util.entityutils;import Android.app.Activity;import Android.os.asynctask;import Android.os.bundle;import Android.view.view;import Android.view.View.OnClickListener; Import Android.widget.button;import Android.widget.textview;public class Mainactivity extends Activity implements Onclicklistener {private Button main_button;private TextView main_text; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);//Initialize Control Main_ button =(Button) This.findviewbyid (R.id.main_button); main_text = (TextView) This.findviewbyid (r.id.main_text);// Set the monitor Main_button.setonclicklistener for the button (this);} @Overridepublic void OnClick (View arg0) {//TODO auto-generated method stub//instantiation Asynctaskmytask mt = new MyTask (); mt.exec Ute ();} Inner class, inheriting Asynctaskclass MyTask extends Asynctask<string, string, string> {@Overrideprotected string Doinbackground (String ... arg0) {//TODO auto-generated method Stubhttpclient HC = new Defaulthttpclient (); HttpPost hp = new HttpPost ("http://www.baidu.com");//Post carry parameters list<namevaluepair> list = new arraylist< Namevaluepair> () List.add (New Basicnamevaluepair ("UID", "uname")) List.add (New Basicnamevaluepair ("pwd", " 111111 ")); try {urlencodedformentity et = new Urlencodedformentity (list,http. UTF_8);//Put parameters in HttpPost hp.setentity (ET); HttpResponse hr = Hc.execute (HP); Httpentity he = hr.getentity (); String str = entityutils.tostring (He, "Utf-8");p ublishprogress (str);} catch (Exception e) {//TODO Auto-geNerated catch Blocke.printstacktrace ();} return null;} @Overrideprotected void oncancelled (String result) {//TODO auto-generated method stubsuper.oncancelled (result);} @Overrideprotected void OnPostExecute (String result) {//TODO auto-generated method Stubsuper.onpostexecute (result);} @Overrideprotected void Onprogressupdate (String ... values) {//TODO auto-generated method Stubsuper.onprogressupdate ( values);//Set to TextView Main_text.settext (Values[0]);}}
Run:



Eighth chapter, Post network request demo (Android)

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.