android--Network GET request +post request
Get data with get request??
Adding or deleting changes to modify data with a POST Request
Package Com.example.jreduch07;import Android.os.asynctask;import Android.os.bundle;import Android.support.v7.app.appcompatactivity;import Android.view.view;import Android.widget.button;import Android.widget.edittext;import Android.widget.textview;import Java.io.ioexception;import Java.io.InputStream; Import Java.io.outputstream;import Java.net.httpurlconnection;import Java.net.malformedurlexception;import Java.net.url;public class Httpactivity extends appcompatactivity {public EditText et; Private Button Search; Private Button search1; Private TextView Show; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_http); et= (EditText) Findviewbyid (r.id.et); Search= (Button) Findviewbyid (R.id.search); search1= (Button) Findviewbyid (R.ID.SEARCH1); show= (TextView) Findviewbyid (r.id.show); Search.setonclicklistener (New View.onclicklistener () {@OveRride public void OnClick (view view) {String url= "Http://192.168.1.48:8080/HttpTest/index.jsp?o Ption=getuser&uname=jerehedu "; New Mygetjob (). Execute (URL); } }); Search1.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) { String[] Arg=new string[2]; Arg[0]= "http://192.168.1.48:8080/HttpTest/index.jsp"; Arg[1]= "option=getuser&uname=jerehedu"; New MyPostJob1 (). Execute (ARG); } }); } private class MyPostJob1 extends asynctask<string,void,string>{@Override protected String Doinbackgroun D (String ... strings) {httpurlconnection con=null; InputStream Is=null; StringBuilder sbd=new StringBuilder (); try {URL url=new url (strings[0]); con= (HttpURLConnection) url.openconnection (); Con.setconnecttimeout (5*1000); Con.setreadtimeout (5*1000); Con.setrequestmethod ("POST"); Con.setdoinput (TRUE); Con.setdooutput (TRUE); Con.setusecaches (FALSE); Con.setrequestproperty ("Charset", "UTF-8"); Con.setrequestproperty ("Content-type", "application/x-www-form-urlencoded");//Con.setrequestproperty ("Charset", "UTF-8");//Con.setrequestproperty ("Content-type", "application/x-www-from-urlencoded"); The params should be like this = "option=getuser&uname=jerehedu String params=strings[1]; OutputStream Os=con.getoutputstream (); Data transmission Stream Os.write (params.getbytes ()); Os.flush (); Os.close (); if (Con.getresponsecode () ==200) {is=con.getinputstream (); int next = 0; Byte[] B = new byte[1024]; while (next = Is.read (b)) > 0) {sbd.append (new String (b, 0, next)); } }} catch (Malformedurlexception e) {e.printstacktrace (); } catch (IOException e) {e.printstacktrace (); }finally {if (is!=null) {try {is.close (); } catch (IOException e) {e.printstacktrace (); } if (Con!=null) {con.disconnect (); Disconnect}}} return Sbd.tostring (); } @Override protected void OnPostExecute (String s) {Super.onpostexecute (s); Show.settext ("POST request Result:" +s); }/* Asynctask asynchronous task Class The first parameter of the asynchronous task class is passed to the third parameter in the Doinbackground method #指定doINBackground方法的返回值 # The return value of the Doinbackground method is received by OnPostExecute */private class Mygetjob extends Asynctask<string, Void, string> { OnPreExecute the initialization of the command//progress bar in the main thread @Override protected void OnPreExecute () { SUper.onpreexecute (); //doinbackground executes the name command in a child thread @Override protected string Doinbackground (String ... strings) {HttpURLConnection con = null; InputStream is = null; StringBuilder SBD = new StringBuilder (); try {URL url = new URL (strings[0]); Con = (httpurlconnection) url.openconnection (); Con.setconnecttimeout (5 * 1000); Con.setreadtimeout (5 * 1000); /* *http corresponding 200: Success * 404 Not Found * 500 error occurred */if (Con.getresponsecode () = = 200) {is = Con.getinputstream (); int next = 0; byte[] bt = new byte[1024]; while (next = Is.read (BT)) > 0) {sbd.append (new String (BT, 0, next)); } }} catch (Malformedurlexception e) {e.printstacktrace (); } catch (IOException e) {e.printstacktrace (); } finally {if (is = null) {try {is.close (); } catch (IOException e) {e.printstacktrace (); } if (con! = null) {con.disconnect (); Disconnect}}} return Sbd.tostring (); //onpostexecute executes the command main thread in the UI thread @Override protected void OnPostExecute (String s) { Super.onpostexecute (s); Show.settext (s); } } }
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout 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" tools:context= "com.example.jreduch07.HttpActivity" > <textview android:layout_width= "Wra P_content "android:layout_height=" wrap_content "android:text=" Name! " Android:textsize= "20DP" android:layout_aligntop= "@+id/et" android:layout_alignparentstart= "true" Android Oid:layout_alignbottom= "@+id/et" android:id= "@+id/textview"/> <edittext android:layout_width= "wrap _content "android:layout_height=" wrap_content "android:hint=" Please enter the name you want to find "android:id=" @+id/et "an Droid:layout_alignbottom= "@+id/search" android:layout_centerhorizontal= "true"/> <button Android:la Yout_width= "Wrap_content" android:layout_height= "Wrap_contenT "android:text=" query (Get method) "android:id=" @+id/search "android:layout_alignparenttop=" true "Android Oid:layout_alignparentend= "true"/> <textview android:layout_width= "Match_parent" android:layout_he ight= "150DP" android:background= "#fff30d" android:id= "@+id/show" android:textsize= "30SP" Andro id:text= "Query Result:" android:layout_below= "@+id/search1" android:layout_alignparentstart= "true"/> <Butt On android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "Query (POST method)" Android:id= "@+id/search1" android:layout_below= "@+id/et" android:layout_alignparentend= "true"/>& Lt;/relativelayout>
android--Network GET request +post request