First, the requirements document is as follows:
Second, the Java code is as follows
Public classMainactivityextendsappcompatactivity {26Private Final intPost_value = 1; The String text = ""; //the ID is not available here because it is not connected to Activity_main,xmlTextView TextView; //--------------------------------------------define a handler to process the message---------------------------------------------- FinalHandler Handler =NewHandler () {@Override Public voidhandlemessage (Message message) {Switch(Message.what) {54 CasePOST_VALUE:textView.setText (Text= (text + "=" +message.obj)); Text= ""; Break; 103default: Break; } } }; //-----------------------------------------------------------------------------------------------------@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); TextView= (TextView) Findviewbyid (R.id.textview); 131
//-------------------------------------------Set the listening--------------------------------------------------for symbol =Button Sendget =(Button) Findviewbyid (r.id.send); Sendget.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {152NewThread (NewRunnable () {@Override Public voidrun () {if(strtmp.length==2) {Calbean TB=NewCalbean (10, 2, plus); Gson Gson=NewGson (); //parameters passed inString datas =Gson.tojson (TB); String URL= "Http://100.108.129.56:8080/example/cal"; String Data=sendpostrequest (URL, datas); Message Message=NewMessage (); Message.what=Post_value; Message.obj=data.tostring (); Handler.sendmessage (message); }}). Start (); } Catch(Exception e) {log.i ("OK", "There must be something wrong!"); return; } } }); //----------------------------------------------------------------------------------------------------- } Public Staticstring sendpostrequest (string url, string param) {httpurlconnection httpurlconnection=NULL; OutputStream out=NULL;//WriteInputStream in =NULL;//Read intResponsecode = 0;//HTTP status code for remote host responseString result = ""; Try{URL Sendurl=Newurl (URL); HttpURLConnection=(HttpURLConnection) sendurl.openconnection (); //Post mode requestHttpurlconnection.setrequestmethod ("POST"); //Set Header informationHttpurlconnection.setrequestproperty ("Headerdata", "Ceshiyongde"); //be sure to set Content-type or the server will not receive parametersHttpurlconnection.setrequestproperty ("Content-type", "Application/json; Charset=utf-8 "); //indicates that the application will write data to a URL connection with the default value of false (whether to pass the parameter)Httpurlconnection.setdooutput (true); //Httpurlconnection.setdoinput (true);Httpurlconnection.setusecaches (false); Httpurlconnection.setconnecttimeout (30000);//30-Second connection timeoutHttpurlconnection.setreadtimeout (30000);//30-Second Read timeout//Incoming Parametersout =Httpurlconnection.getoutputstream (); Out.write (Param.getbytes ()); Out.flush (); //empties the buffer, sends the dataOut.close (); Responsecode=Httpurlconnection.getresponsecode (); //get the requested resourceBufferedReader br =NewBufferedReader (NewInputStreamReader (Httpurlconnection.getinputstream (), "UTF-8")); Result=Br.readline (); } Catch(Exception e) {e.printstacktrace (); } Map<string, string> map =NewGson (). Fromjson (Result,NewTypetoken<map<string, string>>() {}.gettype ()); returnMap.get ("Result"); }}
Public classCalbean {Private floatpara1; Private floatPara2; PrivateString opt; PublicCalbean (floatPARA1,floatPara2, String opt) { This. para1 =para1; This. Para2 =Para2; This. opt =opt; } PublicCalbean () {}; Public floatgetpara1 () {returnpara1; } Public floatGetPara2 () {returnPara2; } PublicString getOpt () {returnopt; } Public voidSETPARA1 (floatpara1) { This. para1 =para1; } Public voidSetPara2 (floatpara2) { This. Para2 =Para2; } Public voidsetopt (String opt) { This. opt =opt; } @Override PublicString toString () {return"calbean{" + "para1=" + Para1 + ", para2=" + Para2 + ", opt= '" + opt + ' \ "+ ‘}‘; }}
Third, the interface layout is as follows
<?xml version= "1.0" encoding= "Utf-8"? ><gridlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:id= "@+id/activity_main"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:rowcount= "7"Android:columncount= "4"Tools:context= "Com.example.weihy.fourfour.MainActivity" > <TextView android:layout_width= "Match_parent"Android:layout_height= "100DP"Android:id= "@+id/textview"Android:layout_columnspan= "4"/> <Button android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text="="Android:id= "@+id/send"/></gridlayout>
Iv. Open Network Requests
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.example.weihy.fourfour" > <uses-permission android:name= "Android.permission.INTERNET"/> <Application Android:allowbackup= "true"Android:icon= "@mipmap/ic_launcher"Android:label= "@string/app_name"Android:supportsrtl= "true"Android:theme= "@style/apptheme" > <activity android:name= ". Mainactivity "> <intent-filter> <action android:name=" Android.intent.action.MAIN "/&G T <category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity& Gt </application></manifest>
"Android" uses Gson and post requests and server communication