Android HTTP Network programming (II)

Source: Internet
Author: User

The previous article simply introduced the two modes of communication of HTTP in Android, and used to get Baidu Web page to do an example. But in actual application, more is the client through the request parameter to implement the concrete operation on the service side, and finally return the data to the client. Because it is impossible for us to set a constant network address for every operation that requires a network request from the client.

(because I have not really learned Java Web from. NET, so the code of the Web is relatively simple, we forgive.) )

Let's do a simple small example: (Simple user login)

Here we will use the method for Tomcat, as well as genimotion to get the local IP. For children who are completely unfamiliar with this, see another blog, "Simple configuration and use of Tomcat in Android."

First we need to complete a server-side code:

<%@ page contenttype= "text/html; CHARSET=GBK "language=" java "%><%String param; Param= Request.getparameter ("param"); if(!"". Equals (param) | | param!=NULL) {        if("Get". Equals (param)) {Out.println ("Send GET request Succeeded"); }Else if("POST". Equals (param)) {String content=request.getparameter ("Content"); String Nickname=request.getparameter ("nickname"); if(content!=NULL&&nickname!=NULL) {Nickname=NewString (Nickname.getbytes ("iso-8859-1"), "Utf-8"); Content=NewString (Content.getbytes ("iso-8859-1"), "Utf-8");                if(Nickname.equals ("admin") &&content.equals ("123456")) {out.println ("Successful Landing!" "); }            }        }    }%>

I named this file test.jsp and placed it under the specified path of Tomcat to use Tomcat as the server to run test.jsp.
Note: The data I return here is GBK, not utf-8.

Next, we'll complete the Android client code:

Here's how we use the HttpClient:

/*** Form Request Implementation Login*/     Public voidtestparamshtppclient () {NewThread (NewRunnable () {inti = 0; PrivateString str; @Override Public voidrun () {HttpClient client=Newdefaulthttpclient (); //Set PathHttpPost request =NewHttpPost ("http://192.168.1.100:8080/mytest/test.jsp");                HttpResponse response; //set up a formList<namevaluepair> params=NewArraylist<namevaluepair>(); Params.add (NewBasicnamevaluepair ("param", "post"));//Request MethodParams.add (NewBasicnamevaluepair ("nickname", "admin"));//User nameParams.add (NewBasicnamevaluepair ("Content", "123456"));//Password                Try{request.setentity (NewUrlencodedformentity (params, "utf-8"));//Setting the Encoding methodResponse =Client.execute (Request); if(Response.getstatusline (). Getstatuscode () = =HTTPSTATUS.SC_OK) {STR=entityutils.tostring (Response.getentity ()). Trim (); }Else{str= "Request Failed"; } System.out.println (str);//Print Back Results                } Catch(clientprotocolexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace ();    }}). Start (); }


Open the Tomcat server and write the above method into the activity's OnCreate () method. (Execute when activity is created)

The results of the operation are as follows:

OK, a small example of the interaction with the server parameter is written to complete. I believe that learning the Java web of children's shoes than I can write better! In this way, at least interacting with the server, we can do it by writing some simple code, no longer tied to a server that the school has no dedicated student contact with.

Unknowingly to the early morning, the first blessing of our great Motherland Mother Happy Birthday!!!

Demo Download: http://download.csdn.net/detail/af74776/7994837

Android HTTP Network programming (II)

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.