This article mainly introduces how Xamarin uses get to pass the JSON value to PHP server login page, the interested friend's reference, hope to be helpful to everyone.
Recently, the time to write a use Xamarin to write an android JSON to the PHP server program, as a login page to use, Need System.json components, PHP directly get to the data into the JSON, not much to say, on the code, first make a PHP server, LAN can be used across the network segment, pro-Test.
This is the 6.php page code on the service side
<?php//array for JSON response$response = Array ();//Check for required Fieldsif (isset ($_get[' name ']) && Isse T ($_get[' password ')) {$name = $_get[' name ']; $pwd = $_get[' password '); if ($name = = "AAA" & $pwd = = "222") {//successfully inserted into database $response ["success"] = 1; $response ["message"] = "sucess"; Echoing JSON response echo Json_encode ($response); $myfile = fopen ("Newfile.txt", "w") or Die ("Unable to open file!"); $txt = "Success"; Fwrite ($myfile, $txt); fclose ($myfile); } else {//failed to insert row $response ["success"] = 0; $response ["message"] = "name or password is wrong"; Echoing JSON response echo Json_encode ($response); $myfile = fopen ("Newfile.txt", "w") or Die ("Unable to open file!"); $txt = "User name or password error"; fwrite ($myfile, $txt); fclose ($myfile); }} else {//required field is missing $response ["success"] = 0; $response ["message"] = "the Requireis not compition "; Echoing JSON response echo Json_encode ($response); $myfile = fopen ("Newfile.txt", "w") or Die ("Unable to open file!"); $txt = "Missing"; Fwrite ($myfile, $txt); fclose ($myfile); }?>
The code for Xamarin Android is as follows:
First, the code for the UI interface.
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:orientation=" vertical "android:layout_width=" match_parent "android:layout_height=" Match_parent "& Gt <linearlayout android:orientation= "Horizontal" android:layout_width= "Match_parent" Android:layout_ height= "Wrap_content" android:id= "@+id/linearlayout1" > <textview android:text= "username:" Android:textappearance= "? Android:attr/textappearancelarge" Android:layout_width= "Wrap_content" android:layout_height= "Match_parent" android:id= "@+id/textview1"/> <edittext android:i Nputtype= "Textmultiline" android:layout_width= "match_parent" android:layout_height= "Match_parent" Android:id= "@+id/edittext1"/> </LinearLayout> <linearlayout android:orientation= "Horizo Ntal "Android:layout_Width= "Match_parent" android:layout_height= "wrap_content" android:id= "@+id/linearlayout3" > <Tex TView android:text= "Password:" android:textappearance= "? Android:attr/textappearancelarge" Andro Id:layout_width= "Wrap_content" android:layout_height= "match_parent" android:id= "@+id/textview2"/> ; <edittext android:inputtype= "Date" android:layout_width= "Match_parent" Android:layout_ height= "Match_parent" android:id= "@+id/edittext2"/> </LinearLayout> <linearlayout and roid:orientation= "Horizontal" android:layout_width= "Match_parent" android:layout_height= "Wrap_content" Android:id= "@+id/linearlayout2" android:gravity= "center" > <button android:text= "Login" Android:layout_width= "Wrap_content" android:layout_height= "match_parent" android:id= "@+id/butt On1 "/> ≪/linearlayout></linearlayout>
MainActivity.cs's Code
Using android.app;using android.widget;using android.os;using system.json;using system.net;using System.IO;using system;using android.content;using system.linq;namespace app10{[Activity (Label = "App10", Mainlauncher = true)] Pub Lic class Mainactivity:activity {Button btn_login; EditText Txt_name; EditText txt_pwd; protected override void OnCreate (Bundle savedinstancestate) {base. OnCreate (savedinstancestate); Set our view from the "main" layout resource Setcontentview (Resource.Layout.Main); Btn_login = findviewbyid<button> (Resource.Id.button1); Txt_name = findviewbyid<edittext> (RESOURCE.ID.EDITTEXT1); Txt_pwd = findviewbyid<edittext> (RESOURCE.ID.EDITTEXT2); Btn_login. Click + = Btn_lgoin_click; private void Btn_lgoin_click (object sender, System.EventArgs e) {string name = Txt_name. Text; string pwd = Txt_pwd. Text; if (string. IsNullOrEmpty (name) | | String. IsNullOrEmpty (pwd)) {Toast.maketext (this, "Please enter your username and password!!) ", Toastlength.long). Show (); Return } else {//json request URL get method String url = "Http://192.168.0.45:8081/js Ontest/6.php?name= "+name+" &password= "+PWD; Create a request var httpreq = (HttpWebRequest) httpwebrequest.create (new Uri (URL)); Httpreq.begingetresponse (New AsyncCallback (Readxamarin), httpreq); }}//Async callback method public void Readxamarin (IAsyncResult asyn) {var httpreq = (httpweb Request) Asyn. asyncstate; Get response using (var httpres = (HttpWebResponse) httpreq.endgetresponse (Asyn)) {//judgment Yes No successful get response if (Httpres.statuscode = = Httpstatuscode.ok) {//Read response var text = new StreamReader (Httpres.getresponsestream ()). ReadToEnd (); Switch to the UI thread, otherwise the control cannot be manipulated runonuithread (() = {Toast.maket Ext (this, text, Toastlength.long). Show (); }); } } }}}