Original: Uncle also said Xamarin~android ~ Call the remote API interface, send a POST request
Xamarin We've taught you how to deploy it in the last section, a practical example today, using an Android client to invoke an interface to the. NET Web API and send a POST request that responds when the server returns to the request. Client Android will respond to the output of the content, and jump to a different activity page, loading the page content on the new view to the WebView control, the entire process is over!
Add several form elements, account numbers, passwords, login buttons to the main page and assign them to the activity class's OnCreate method and add the button's Click event
protected Override voidOnCreate (Bundle savedinstancestate) {Base. OnCreate (savedinstancestate); //Set Our view from the "main" layout resourceSetcontentview (Resource.Layout.Main); //Get Our buttons from the layout resource,//And attach an event to itButton button = findviewbyid<button>(Resource.Id.myButton); button. Click+=Delegate{button. Text=string. Format ("{0} clicks!", count++); }; Android.Util.Log.Info ("Normal","Log Zzl"); varLOGINBTN = findviewbyid<button>(RESOURCE.ID.LOGINBTN); varUsername = findviewbyid<textview>(Resource.Id.username); varPassword = findviewbyid<textview>(Resource.Id.password); varresult = Findviewbyid<textview>(Resource.Id.result); Loginbtn.click+=Delegate { stringURL ="Http://api.xuexiba.com/v1/User/Login"; //Create httpclient (note incoming httpclienthandler) using(varHTTP =NewHttpClient ()) { varContent =NewFormurlencodedcontent (Newdictionary<string,string> () { { "username", username. Text}, {"Password", password. Text}}); varResponse =http. Postasync (URL, content); Result. Text=Response. Result.Content.ReadAsStringAsync (). Result; Intent Intent=NewIntent ( This,typeof(viewpageactivity)); StartActivity (Intent); } }; }
Two in viewpageactivity add a webview to display the contents of the Web page, the following code
protected Override voidOnCreate (Bundle savedinstancestate) {Base. OnCreate (savedinstancestate); Setcontentview (Resource.Layout.ViewPage); varWebView = findviewbyid<webview>(Resource.Id.webView); //enabling JavaScript EnablewebView.Settings.JavaScriptEnabled =true; //Loading URLsWebview.loadurl ("http://www.sina.com"); //open directly on the current WebViewWebview.setwebviewclient (Newcustwebviewclient ()); }
Note that the code webview.setwebviewclient (new custwebviewclient ()) indicates that the Web page content is loaded with the existing webview, and if this line is not added, the page will be loaded using the system's own browser.
Download a look at the contents of the Custwebviewclient class
Public class custwebviewclient:webviewclient { publicoverrideboolstring URL) { view. Loadurl (URL); return true ; } }
OK, when we design the page, you can drag it directly from the Toolbox, and the final layout
Finally the APK is generated, and our package is finished!
Uncle also said Xamarin~android article ~ Call the remote API interface, send a POST request