Make a program to check the attribution of the mobile phone number (using the HttpURLConnection post submission method)

Source: Internet
Author: User
Tags response code stringbuffer

Requirements: Use the Post method to provide mobile phone number to the server, to find the place of the mobile phone number

Ideas:
There is a Web server on the network dedicated to providing the attribution information of the mobile phone number,
All we have to do is access the server, provide the phone number, and get the response code from the server.

Resolves the required attribution information from the response code.


Steps:
1. Define the layout file and add the permissions.
2, get the control you want in the main thread.
3, open a child thread, define the good one URL in the child thread, and use the URL object to get a HttpURLConnection connection object.
4, set the request mode for the Connection object, request time-out time, read time-out.
5, set a permission for the connection to write data to the server, and then start writing data to the server to send the phone number information to the server
6, connect the server, get the return code of the server, determine if the connection is successful, if the return code is 200, start to get the data sent from the server
7, define a handler in the main thread and override the Handlemessage method.
8, in a sub-thread, send data sent by the server to the main thread using SendMessage

9, after the handlemessage in the main thread receives the message, the information is displayed on the interface.


Note: When a post is submitted, data is sent to the server before it can start connecting.
That
Conn.setdooutput (TRUE);
OutputStream out = Conn.getoutputstream ();
Out.write (Data.getbytes ());
Must be placed in
Conn.connect (); front.
Otherwise it will be reported Java.lang.IllegalStateException:Already connected

Code:

public class Mainactivity extends Activity {
    private EditText EditText;
    Private TextView TextView;
    7, define a handler in the main thread and override the Handlemessage method. Private Handler Handler = new Handler () {public void Handlemessage (message msg) {//9, after the handlemessage in the main thread receives the information, the information
			displayed on the interface.
			String text = (string) msg.obj;
		Textview.settext (text);
    }    	
    };
        public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
        
        Setcontentview (R.layout.main);
        2, get the control you want in the main thread.
        EditText = (editText) Findviewbyid (R.id.edittext);
    TextView = (TextView) Findviewbyid (R.id.textview); }//Click event public void OnClick (View v) {//Get the phone number in the input box final String phone = Edittext.gettext (). Tostrin
    	g ();
    	Final String data = "mobilecode=" +phone+ "&userid=";
    	Final String url = "Http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";
    	3, open a child thread, define the good one URL in the child thread, and use the URL object to get a HttpURLConnection connection object. New Thread (New Runnable () {public void run () {try {URL u = new URL (URL);
					HttpURLConnection conn = (httpurlconnection) u.openconnection ();
					4, set the request mode for the Connection object, request time-out time, read time-out.
					Conn.setrequestmethod ("POST");//Still uppercase conn.setreadtimeout (5000);
					
					Conn.setconnecttimeout (5000);
					5, set a permission to write data to the server, and then start writing data to the server, sending the data to the server Conn.setdooutput (TRUE);
					OutputStream out = Conn.getoutputstream ();
					Out.write (Data.getbytes ());
					6, connect the server, get the return code of the server, determine if the connection is successful, if the return code is 200, then start to get the data sent from the server Conn.connect ();
					int responsecode = Conn.getresponsecode ();
						if (Responsecode = =) {InputStream is = Conn.getinputstream ();
						A custom method for extracting data sent through the input stream, String text = Getstringfrominputstream (IS);
						8, in a child thread, sends the data sent by the server using handler to the main thread in message msg = new Message ();
						Msg.obj = text;
					Handler.sendmessage (msg);
					} out.close ();
				Conn.disconnect ();
				} catch (Exception e) {e.printstacktrace ();
    }}). Start (); }//Custom method to extract the required information from the incoming input stream and return to Protected String Getstringfrominputstream (InputStream is) throws Exception {BufferedReader br = new BufferedReader (New INP
		Utstreamreader (IS));
		StringBuffer sb = new StringBuffer ();
		String text = null;
		while ((Text = Br.readline ()) = null) {Sb.append (text+ "/r/n");
		} Text = Sb.tostring ();
		int start = Text.indexof ("\" > ") +2;
		int end = Text.indexof ("</");
		Text = text.substring (start, end);
		Br.close ();
	return text; }
}

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.