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)