recently in the learning of Android network programming knowledge, the following is some of the learning experience tonight. Share with you.
in the actual application development many times need the app to request the data to the server, then how does the app send the request? The following code is one of the cases where you use HttpURLConnection to send the requested data to the server and then process the data returned by the server.
The following code is just a simple test code for the client. Also need to install a Tomcat server on the PC, and then deploy the JSP file in the demo to Tomcat, the specific way Baidu will know.
Package Com.lee.nethttp;import Java.io.bytearrayoutputstream;import Java.io.ioexception;import Java.io.InputStream ; Import Java.io.printwriter;import Java.io.unsupportedencodingexception;import Java.net.httpurlconnection;import Java.net.url;import Android.app.activity;import Android.os.asynctask;import Android.os.bundle;import Android.util.base64;import Android.util.log;import Android.view.view;import Android.view.View.OnClickListener; Import Android.widget.button;import Android.widget.edittext;import Android.widget.textview;public class Mainactivity extends Activity {private EditText medittext = null; Private Button Mbutton = null; Private TextView Mtextview = null; Private String Murl = "http://192.168.0.109:8080/lee/index.jsp";//based on the IP settings of its host @Override protected void OnCreate (Bund Le Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Medittext = (EditText) Findviewbyid (R.id.edit); Mbutton =(Button) Findviewbyid (R.id.button); Mbutton.setonclicklistener (Mbuttonclicklistener); Mtextview = (TextView) Findviewbyid (R.id.text); } private Onclicklistener Mbuttonclicklistener = new Onclicklistener () {@Override public void OnClick (Vi EW v) {String content = Medittext.gettext (). toString (); Content = toBase64 (content); String[] params = {content}; New Nethttpasynctask (). Execute (params); } }; Private class Nethttpasynctask extends Asynctask<string, Void, string> {@Override protected String do Inbackground (String ... params) {string result = NULL; try {URL url = new URL (murl); HttpURLConnection connection = (httpurlconnection) url.openconnection (); Connection.setrequestmethod ("POST"); Connection.setdoinput (TRUE); Connection.setdooutput (TRUE); Connection.setusecaches (FALSE); Connection.setrequestproperty ("Content-type", "application/x-www-form-urlencoded;charset=utf-8"); Connection.setconnecttimeout (3000); Connection.setreadtimeout (3000); PrintWriter writer = new PrintWriter (Connection.getoutputstream ()); String str = "Content" + "=" + params[0]; Writer.print (str); Writer.flush (); Writer.close (); InputStream input = Connection.getinputstream (); Bytearrayoutputstream OutputBuffer = new Bytearrayoutputstream (); int ch; while (ch = input.read ())! =-1) {outputbuffer.write (CH); } result = Outputbuffer.tostring (); } catch (IOException e) {e.printstacktrace (); } return Result } @Override protected void OnPostExecute (String result) {Mtextview.settext (result); }} private string ToBase64 (string content) {try {content = base64.encodetostring (content.get Bytes ("UTF-8"), Base64.default); LOG.E ("Lee", "toBase64 content =" + content); } catch (Unsupportedencodingexception e) {e.printstacktrace (); } return content; }}
Demo
HTTP Send/Request service for Android network programming