Android uses HTTP request phone number attribution to query code sharing _android

Source: Internet
Author: User

Attribution to the data source

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

The Webxml Web site also supports other requests such as soap and so on

The interface is relatively simple

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns: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" > <textview android:layout_ Width= "Fill_parent" android:layout_height= "Wrap_content" android:text= "mobile number:"/> <edittext android:id= "@+id/ Phone_sec "android:layout_width=" fill_parent "android:layout_height=" Wrap_content "android:inputType=" Textphonetic "android:singleline=" true "android:hint=" at least the first seven bits/> <button android:id= "@+id/query_btn" Android:
Layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "right" android:text= "query" /> <textview android:id= "@+id/result_text android:layout_width=" wrap_content "android:layout_height=" Wrap_  Content "android:layout_gravity=" center_horizontal|center_vertical "/> </LinearLayout>

Below is the Mainactivity.java

Package Com.sphere.guishudi;
Import java.util.ArrayList;
Import java.util.List;
Import Org.apache.http.HttpResponse;
Import Org.apache.http.HttpStatus;
Import Org.apache.http.NameValuePair;
Import org.apache.http.client.HttpClient;
Import org.apache.http.client.entity.UrlEncodedFormEntity;
Import Org.apache.http.client.methods.HttpPost;
Import org.apache.http.impl.client.DefaultHttpClient;
Import Org.apache.http.message.BasicNameValuePair;
Import Org.apache.http.protocol.HTTP;
Import Org.apache.http.util.EntityUtils;
Import android.app.Activity;
Import Android.app.ProgressDialog;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.os.Message;
Import Android.view.Menu;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.EditText;
Import Android.widget.TextView; 
/** * Mobile number attribution to the query * * public class Mainactivity extends activity {private EditText phonesecedittext; 
Private TextView Resultview; Private Button Querybutton;
Private ProgressDialog Prodialog;
private thread thread;
Define message private static final int number_format_error = 0;
private static final int query_success_msg = 1; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (
R.layout.main); 
Phonesecedittext = (edittext) Findviewbyid (R.ID.PHONE_SEC); 
Resultview = (TextView) Findviewbyid (R.id.result_text);
Querybutton = (Button) Findviewbyid (R.ID.QUERY_BTN);
Prodialog = new ProgressDialog (mainactivity.this);
Prodialog.settitle ("Place of Enquiry");
Prodialog.setmessage ("You are inquiring, please wait patiently ...");
Querybutton.setonclicklistener (New Queryonclicklistener ()); Handler Handler = new Handler () {@Override public void handlemessage (message msg) {super.handlemessage (msg); switch (ms
G.what) {case NUMBER_FORMAT_ERROR:phoneSecEditText.setText ("");
Resultview.settext ("You entered a wrong number format");
Break
Case QUERY_SUCCESS_MSG:resultView.setText (msg.obj.toString ());
Prodialog.dismiss ();
Break
Default:break; }
}
};
String phonesec; Class Queryonclicklistener implements onclicklistener{@Override public void OnClick (View arg0) {//Get cell phone number phonesec = Phone
Secedittext.gettext (). toString (). Trim (); if ("". Equals (phonesec) | |
Phonesec.length () <7) {//Send message to display query results TextView empty handler.sendemptymessage (number_format_error);//Lock focus
Phonesecedittext.requestfocus ();
Return
//Inquiry Mobile phone number (paragraph) information//getremoteinfo (PHONESEC);
thread = new Thread (new Querythread ());
Thread.Start ();
Prodialog.onstart ();
Prodialog.show (); } class Querythread implements runnable{@Override public void Run () {getremoteinfo (phonesec);}/** * Mobile phone number paragraph attribution to the query * @param phonesec Mobile Phone segment */private void Getremoteinfo (String phonesec) {//TODO auto-generated Method Stub//define pending URL St
Ring Requesturl = "Http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";
Create HttpClient instance HttpClient client = new Defaulthttpclient ();
Creates a HttpPost instance based on the URL httppost post = new HttpPost (Requesturl); list<namevaluepair> params =New Arraylist<namevaluepair> ();
Sets the parameters to be passed Params.add (new Basicnamevaluepair ("Mobilecode", phonesec));
Params.add (New Basicnamevaluepair ("UserId", "")); try {//Set URL encoding post.setentity (new urlencodedformentity (params, HTTP).
Utf_8));
Send request and get feedback httpresponse response = Client.execute (POST); Determine if the request successfully processed if (Response.getstatusline (). Getstatuscode () = = HTTPSTATUS.SC_OK) {//parse returned content String result = entityutils.t
Ostring (Response.getentity ());
The query result is parsed and displayed in TextView//resultview.settext (filterhtml (result));
msg = new Message ();
Msg.what = query_success_msg;
Msg.obj = filterhtml (result);
Handler.sendmessage (msg);
The catch (Exception e) {e.printstacktrace ();}} /** * Use regular expressions to filter HTML tags * * @param source to filter content * @return/private string filterhtml (string source) {if (null = so 
Urce) {return ""; Return Source.replaceall ("</?[ 
^>]+> "," "). Trim (); @Override public boolean Oncreateoptionsmenu (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 access to the network.

Use child threads to access network query data, handler do message processing.

This is all about httpclient most basic functions (initiating post requests), and most of the operations we perform on the browser client httpclient can be emulated, such as submitting forms, querying data, uploading documents, page jumps, session storage, and so on.

Getmobilecodeinfo

Access to the national mobile phone number of provinces, regions and mobile phone card type information

Input parameters: Mobilecode = string (cell phone number, minimum first 7 digits), UserID = string (commercial user ID) free user is an empty string; return Data: string (mobile phone number: Province City Mobile card type).

Test results: as follows

Related Article

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.