Android Post (HTTP setup parameters, faux HTML form submission)

Source: Internet
Author: User

 PackageCom.example.testhttpurlcon;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.view.Menu; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Thread TD=NewThread (NewRunnable () {@Override Public voidrun () {//TODO auto-generated Method StubHttpurlcontest Huc =Newhttpurlcontest ();            Huc.testhttpurlcon ();        }        });            Td.start (); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.activity_main, menu); return true; }}

 PackageCom.example.testhttpurlcon;ImportJava.io.BufferedReader;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;Importjava.net.HttpURLConnection;ImportJava.net.URL;Importjava.util.ArrayList;Importjava.util.List;ImportOrg.apache.http.HttpResponse;ImportOrg.apache.http.NameValuePair;Importorg.apache.http.client.HttpClient;Importorg.apache.http.client.entity.UrlEncodedFormEntity;ImportOrg.apache.http.client.methods.HttpPost;Importorg.apache.http.impl.client.DefaultHttpClient;ImportOrg.apache.http.message.BasicNameValuePair;ImportOrg.apache.http.protocol.HTTP;Importorg.apache.http.util.EntityUtils;ImportAndroid.util.Log; Public classHttpurlcontest { Public voidTesthttpurlcon () {Try{String URL= "Http://www.xxxx.com: Port number/xxx"; //Create a connectionHttpClient HttpClient =Newdefaulthttpclient (); HttpPost Post=Newhttppost (URL); //set parameters, faux HTML form submissionlist<namevaluepair> paramlist =NewArraylist<namevaluepair>(); Basicnamevaluepair param1=NewBasicnamevaluepair ("name",                        "XXX"); Basicnamevaluepair param2=NewBasicnamevaluepair ("Pass",                        "XXX");                Paramlist.add (param1);p aramlist.add (param2); Post.setentity (Newurlencodedformentity (Paramlist, HTTP.                Utf_8)); //sends a HTTPPOST request and returns the HttpResponse objectHttpResponse HttpResponse =Httpclient.execute (POST); //To determine the request response status code, the status code of 200 indicates that the server has successfully responded to the client's request                if(Httpresponse.getstatusline (). Getstatuscode () = = 200) {                    //Get return resultsString result =entityutils.tostring (HttpResponse. getentity ());                    SYSTEM.OUT.PRINTLN (result); LOG.I ("SM", result); }        } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }}

<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.example.testhttpurlcon"Android:versioncode= "1"Android:versionname= "1.0" >    <USES-SDKandroid:minsdkversion= "8"android:targetsdkversion= "+" />    <uses-permissionAndroid:name= "Android.permission.INTERNET"/>    <ApplicationAndroid:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" >        <ActivityAndroid:name= "Com.example.testhttpurlcon.MainActivity"Android:label= "@string/app_name" >            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MAIN" />                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />            </Intent-filter>        </Activity>    </Application></Manifest>

Android Post (HTTP setup parameters, faux HTML form submission)

Related Article

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.