Using the Afinal framework to implement HTTP requests to get phone number attribution from the network

Source: Internet
Author: User

About the introduction and use of Afinal framework, please click on the left related article classification, to understand, this article is no longer repeat.

This article is mainly to use the afinal framework, to achieve a simple, from the network to obtain the function of the mobile phone attribution to the novice to introduce how to use this framework to complete the interaction with the server.

First, let's introduce the network interface that we need to use.

Http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx

The above Web site is a WebService website, the main function is to provide webservice services externally.


We mainly use the following service, is based on the mobile phone number to obtain information such as attribution.

After clicking into it, we can see the interface

Here, we can see that if we want to invoke this service, we need to pass information such as parameters and return values.

We need to pass two parameters, Mobilecode is the phone number, at least 7 bits, UserID is the ID of the business user, we do not need to pass value.

The return value is returned in the form of a province, city, and mobile card type in XML format.

Put down look, we can find this piece of code



This paragraph is the use of the SOAP protocol call this interface, our data and return value format, because we do not use this way, so we can not focus on the moment, we need to focus on the following this paragraph


In this section, we define the return value of the request format and data that we use get and post two ways to access this interface.

With this information in place, we can write the code.


First of all, look at our layout file, very simple

< LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/    Tools "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" vertical "         android:padding= "10DP" > <edittext android:id= "@+id/et_number" android:layout_width= "Match_parent" android:layout_height= "Wrap_content" android:hint= "phone number"/> <button android:layout_width= "m Atch_parent "android:layout_height=" wrap_content "android:onclick=" GetInfo "android:text=" to get attribution information "/&    Gt <textview android:id= "@+id/tv_info" android:layout_width= "match_parent" android:layout_height= "Wra P_content "android:gravity=" center "android:textcolor=" @android: Color/holo_blue_light "Android:textsi Ze= "20sp"/></LINEARLAYOUT> 



Here is the code implementation, because the use of the afinal framework, so if you have questions about this part, please also check the left article category

/** * Get phone Attribution */public class Mainactivity extends Finalactivity {@ViewInject (id = r.id.tv_info) private TextView Tv_info;@v Iewinject (id = r.id.et_number) private EditText et_number;private finalhttp http;private Context mcontext;@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); mcontext = This;http = new Finalhttp ();} Click event for button public void GetInfo (View v) {requestbypost ();} Gets the private void Requestbyget () {String number = Et_number.gettext () by the Get method. ToString (). Trim (); String url = "Http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo"; if (Number.isempty ( ) {Toast.maketext (Mcontext, "Please fill in the phone number", Toast.length_short). Show (); else {ajaxparams params = new Ajaxparams ();p arams.put ("Mobilecode", number);p arams.put ("UserID", ""); Http.get (URL, params, new ajaxcallback<string> () {@Overridepublic void onsuccess (String t) {super.onsuccess (t); tv_ Info.settext (t);} @Overridepublic void OnfailuRe (Throwable t, int errorno, String strMsg) {super.onfailure (T, Errorno, STRMSG); Tv_info.settext ("Error code:" + Errorno + "error message: "+ STRMSG);}});}} Gets the private void Requestbypost () {String number = Et_number.gettext () by post. ToString (). Trim (); String url = "Http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo"; if (Number.isempty ( ) {Toast.maketext (Mcontext, "Please fill in the phone number", Toast.length_short). Show (); else {ajaxparams params = new Ajaxparams ();p arams.put ("Mobilecode", number);p arams.put ("UserID", ""); Http.post (URL, params, new ajaxcallback<string> () {@Overridepublic void onsuccess (String t) {super.onsuccess (t); tv_ Info.settext (t);} @Overridepublic void OnFailure (Throwable t, int errorno, String strMsg) {super.onfailure (T, Errorno, STRMSG); tv_ Info.settext ("Error code:" + Errorno + "error message:" + STRMSG);}});}}}

The above request address needs to be

/webservices/mobilecodews.asmx/getmobilecodeinfo

This address is preceded by a host name, which is http://webservice.webxml.com.cn/


Run results



Of course, we also need to parse the returned XML, how to do it, please refer to the left Article category "Parsing of XML files"



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.