Uncle also said Xamarin ~ Android ~ Call the Remote API to send a POST request, xamarinandroid
Xamarin we have taught you how to deploy its environment in the previous section. Today we are going to use an actual example called by android users. net web api interface, and send a POST request. When the server returns to the request and responds, the android client outputs the response content and switches to another Activity page, load the webpage content to the webView control on the new view. The whole process is complete!
Add several form elements, accounts, passwords, and login buttons on the home page, assign values to them in the OnCreate method of the Activity class, and add the click Event of the button
Protected override void OnCreate (Bundle savedInstanceState) {base. onCreate (savedInstanceState); // Set our view from the "main" layout resource SetContentView (Resource. layout. main); // Get our button from the layout resource, // and attach an event to it Button button = FindViewById <Button> (Resource. id. myButton); button. click + = delegate {button. text = string. format ("{0} clicks! ", Count ++) ;}; Android. util. log. info ("normal", "log zzl"); var loginBtn = FindViewById <Button> (Resource. id. loginBtn); var username = FindViewById <TextView> (Resource. id. username); var password = FindViewById <TextView> (Resource. id. password); var result = FindViewById <TextView> (Resource. id. result); loginBtn. click + = delegate {string url = "http://api.xuexiba.com/v1/User/Login"; // create HttpClient (note the incoming HttpClientHandler) using (var http = new HttpClient ()) {var content = new FormUrlEncodedContent (new Dictionary <string, string> () {"username", username. text}, {"password", password. text}); var response = http. postAsync (url, content); result. text = response. result. content. readAsStringAsync (). result; Intent intent = new Intent (this, typeof (ViewPageActivity); StartActivity (intent );}};}
2. Add a webView in ViewPageActivity to display the webpage content. The following code:
Protected override void OnCreate (Bundle savedInstanceState) {base. onCreate (savedInstanceState); SetContentView (Resource. layout. viewPage); var webView = FindViewById <WebView> (Resource. id. webView); // Enable Javascript Enable webView. settings. javaScriptEnabled = true; // load the webView URL. loadUrl ("http://www.sina.com"); // open webView directly on the current webView. setWebViewClient (new CustWebViewClient ());}
Note: The Code webView. SetWebViewClient (new CustWebViewClient () indicates to use an existing webView to load webpage content. If this line is not added, the webpage will be loaded using the browser that comes with the system,
Download the content of the CustWebViewClient class.
public class CustWebViewClient : WebViewClient { public override bool ShouldOverrideUrlLoading (WebView view, string url) { view.LoadUrl (url); return true; } }
OK. When we design the page, we can drag it directly from the toolbox.
Finally, the APK is generated, and our package is complete!