Reference article: http://blog.csdn.net/lotusyangjun/article/details/22292445
http://blog.csdn.net/withiter/article/details/19908679
The code is as follows:
PackageTdrhedu.com.edugame;Importandroid.content.Intent;ImportAndroid.graphics.Paint;ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.TextView;ImportJava.io.ByteArrayOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;ImportJava.io.OutputStream;Importjava.io.UnsupportedEncodingException;Importjava.net.HttpURLConnection;ImportJava.net.URL;ImportJava.net.URLEncoder;Importjava.util.ArrayList;ImportJava.util.HashMap;Importjava.util.List;ImportJava.util.Map;/*** Created by Zhaoliang on 2016/6/15.*/ Public classLogin2activityextendsappcompatactivity {Private Static FinalString TAG = "Login2activity"; Button Mlogin=NULL; TextView Mforgetpassword=NULL; TextView Mjustsee=NULL; EditText Maccountname, maccountpasswd; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_login2); View v= Findviewbyid (r.id.mloginguide);//find the ID of the layout you want to set the transparent background toV.getbackground (). Setalpha (70);//0~255 Transparency Value//TextView TV = (TextView) Findviewbyid (R.id.find_password); //tv.setpaintflags (tv.getpaintflags () | Paint.underline_text_flag);Mlogin=(Button) Findviewbyid (R.id.account_login); Mlogin.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {log.d (TAG,"Click"); String Postreceiverurl= "Http://112.126.64.235:9090/company/get"; LOG.V (TAG,"PostURL:" +Postreceiverurl); NewThread (NewRunnable () {@Override Public voidrun () {Maccountname=(EditText) Findviewbyid (r.id.account_name); MACCOUNTPASSWD=(EditText) Findviewbyid (R.id.account_password); String username=Maccountname.gettext (). toString (); String Password=Maccountpasswd.gettext (). toString (); Map<string, string> params =NewHashmap<string, string>(); Params.put ("Username", username); Params.put ("Password", password); String result= Submitpostdata (params, "Utf-8"); LOG.V (TAG,"Result:" +result); }}). Start (); //Intent i = new Intent (login2activity.this, Pannelactivity.class); //startactivity (i); } }); /*Mforgetpassword = (TextView) Findviewbyid (R.id.find_password); Mforgetpassword.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {Intent i = new Intent (login2activity.this, Forgotactivity.class); StartActivity (i); } }); Mjustsee = (TextView) Findviewbyid (R.id.just_see); Mjustsee.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) { } });*/ } /** Function: Send POST request to server * Param:params request body content, encode encoded format * Author: [email protected] */ Public StaticString Submitpostdata (map<string, string>params, String encode) { byte[] data = Getrequestdata (params, encode). ToString (). GetBytes ();//get the request body Try{URL URL=NewURL ("Http://112.126.64.235:9090/company/get"); HttpURLConnection HttpURLConnection=(HttpURLConnection) url.openconnection (); Httpurlconnection.setconnecttimeout (3000);//Setting the connection time-out periodHttpurlconnection.setdoinput (true);//Open the input stream to get data from the serverHttpurlconnection.setdooutput (true);//Open the output stream to submit data to the serverHttpurlconnection.setrequestmethod ("POST");//set up post to submit dataHttpurlconnection.setusecaches (false);//cannot use the cache using Post mode//set the type of the request body is the text typeHttpurlconnection.setrequestproperty ("Content-type", "application/x-www-form-urlencoded"); //set the length of the request bodyHttpurlconnection.setrequestproperty ("Content-length", String.valueof (data.length)); //get the output stream and write data to the serverOutputStream OutputStream =Httpurlconnection.getoutputstream (); Outputstream.write (data); intResponse = Httpurlconnection.getresponsecode ();//get the server's response code if(Response = =HTTPURLCONNECTION.HTTP_OK) {InputStream Inptstream=Httpurlconnection.getinputstream (); returnDealresponseresult (Inptstream);//processing server Response Results } } Catch(IOException e) {e.printstacktrace (); } return""; } /** Function: Package request body Information * Param:params request body content, encode encoding format * Author: [email protected] */ Public StaticStringBuffer Getrequestdata (map<string, string>params, String encode) {StringBuffer StringBuffer=NewStringBuffer ();//storage of Packaged request body information Try { for(Map.entry<string, string>Entry:params.entrySet ()) {Stringbuffer.append (Entry.getkey ()). Append ("="). Append (Urlencoder.encode (Entry.getvalue (), encode)). Append ("&"); } Stringbuffer.deletecharat (Stringbuffer.length ()-1);//Delete the Last "&"}Catch(Exception e) {e.printstacktrace (); } returnStringBuffer; } /** Function: Processing server response result (convert input stream to string) * Param:inputstream server Response Input stream * Author: [Email Protec Ted]*/ Public Staticstring Dealresponseresult (InputStream inputstream) {string Resultdata=NULL;//Storage Processing ResultsBytearrayoutputstream Bytearrayoutputstream =NewBytearrayoutputstream (); byte[] data =New byte[1024]; intLen = 0; Try { while(len = inputstream.read (data))! =-1) {bytearrayoutputstream.write (data,0, Len); } } Catch(IOException e) {e.printstacktrace (); } resultdata=NewString (Bytearrayoutputstream.tobytearray ()); returnResultdata; }}
Complete.
Android POST request