Attribution to Data source
Http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx
Webxml website also supports other request methods such as soap and so on
The interface is relatively simple
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"Android:paddingtop= "5dip"Android:paddingleft= "5dip"Android:paddingright= "5dip" > <TextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "mobile phone number:" /> <EditTextAndroid:id= "@+id/phone_sec"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:inputtype= "Textphonetic"Android:singleline= "true"Android:hint= "Enter at least the first seven bits" /> <ButtonAndroid:id= "@+id/query_btn"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Right"Android:text= "Query" /> <TextViewAndroid:id= "@+id/result_text"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Center_horizontal|center_vertical" /></LinearLayout>
View Code
Here's Mainactivity.java.
PackageCom.sphere.guishudi;Importjava.util.ArrayList;Importjava.util.List;ImportOrg.apache.http.HttpResponse;ImportOrg.apache.http.HttpStatus;ImportOrg.apache.http.NameValuePair;Importorg.apache.http.client.HttpClient;Importorg.apache.http.client.entity.UrlEncodedFormEntity;ImportOrg.apache.http.client.methods.HttpPost;Importorg.apache.http.impl.client.DefaultHttpClient;ImportOrg.apache.http.message.BasicNameValuePair;ImportOrg.apache.http.protocol.HTTP;Importorg.apache.http.util.EntityUtils;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Message;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.TextView;/*** Mobile phone number attribution to search*/ Public classMainactivityextendsActivity {PrivateEditText Phonesecedittext; PrivateTextView Resultview; PrivateButton Querybutton; Privatethread thread; //Defining Messages Private Static Final intNumber_format_error = 0; Private Static Final intQuery_success_msg = 1; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Phonesecedittext=(EditText) Findviewbyid (R.ID.PHONE_SEC); Resultview=(TextView) Findviewbyid (R.id.result_text); Querybutton=(Button) Findviewbyid (R.ID.QUERY_BTN); Querybutton.setonclicklistener (NewQueryonclicklistener ()); } Handler Handler=NewHandler () {@Override Public voidhandlemessage (Message msg) {Super. Handlemessage (msg); Switch(msg.what) { CaseNUMBER_FORMAT_ERROR:phoneSecEditText.setText (""); Resultview.settext ("The number you entered is in the wrong format"); Break; CaseQUERY_SUCCESS_MSG:resultView.setText (msg.obj.toString ()); Break; default: Break; } } }; String phonesec; classQueryonclicklistenerImplementsonclicklistener{@Override Public voidOnClick (View arg0) {//get the phone numberPhonesec =Phonesecedittext.gettext (). toString (). Trim (); if("". Equals (phonesec) | | Phonesec.length () <7){ //send a message to show the results of the query TextView emptyhandler.sendemptymessage (Number_format_error); //Lock FocusPhonesecedittext.requestfocus (); return; } //Search Mobile phone number (segment) Information//Getremoteinfo (phonesec);Thread =NewThread (NewQuerythread ()); Thread.Start (); } } classQuerythreadImplementsrunnable{@Override Public voidrun () {getremoteinfo (phonesec); } } /*** Mobile phone number section attribution to search *@paramphonesec Mobile phone number segment*/ Private voidGetremoteinfo (String phonesec) {//TODO auto-generated Method Stub//define the URL to be requestedString Requesturl = "Http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo"; //Creating an HttpClient instanceHttpClient client =Newdefaulthttpclient (); //Create a HttpPost instance from a URLHttpPost post =NewHttpPost (Requesturl); List<NameValuePair> params =NewArraylist<namevaluepair>(); //set parameters that need to be passedParams.add (NewBasicnamevaluepair ("Mobilecode", phonesec)); Params.add (NewBasicnamevaluepair ("UserId", "" ")); Try { //Set URL encodingPost.setentity (Newurlencodedformentity (params, HTTP. Utf_8)); //send a request and get feedbackHttpResponse response =Client.execute (POST); //determine if the request was processed successfully if(Response.getstatusline (). Getstatuscode () = =HTTPSTATUS.SC_OK) { //parsing the returned contentString result =entityutils.tostring (Response.getentity ()); //The query results are parsed and displayed in TextView//Resultview.settext (filterhtml (Result));Message msg =NewMessage (); Msg.what=query_success_msg; Msg.obj=filterhtml (Result); Handler.sendmessage (msg); } } Catch(Exception e) {e.printstacktrace (); } } /*** Filter HTML tags with regular expressions * *@paramSource to filter content *@return */ Privatestring filterhtml (string source) {if(NULL==source) { return""; } returnSource.replaceall ("</?[ ^>]+> "," "). Trim (); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); return true; }}
Remember to configure <uses-permission android:name= "Android.permission.INTERNET" in Androidmanifest.xml/>
Gives the program permission to access the network.
Using child threads to access network query data, handler do message processing.
The above is just the basic function of httpclient (initiating a POST request), and most of the operations we do in the browser client httpclient can be simulated, for example: Submit a form, query data, upload a document, page jump, session storage, etc.
Getmobilecodeinfo
Get domestic mobile number to the province, region and mobile card type information
Input parameters: Mobilecode = string (mobile number, minimum first 7 digits), UserID = string (business User ID) Free user is an empty string; return Data: String (Mobile number: Province City Mobile card type).
Test Result: The following
Reference: http://blog.csdn.net/lyq8479/article/details/6413216
Android uses HTTP request to query phone number attribution