Uncle also said Xamarin~android article ~ Call the remote API interface, send a POST request

Source: Internet
Author: User

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

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.