Uncle also said Xamarin ~ Android ~ Call the Remote API to send a POST request, xamarinandroid

Source: Internet
Author: User

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!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.