Android client and server data interaction, android Server
There are many examples on the Internet to demonstrate how the Android client interacts with the server. However, these examples are mostly complicated, which is not good for beginners, here are some examples of simple code and clear logic interaction. This blog introduces the fourth type:
I. server side:
Code 1: Add a file named "AndroidServerServlet. Java"
[Java]View plain copy
- Package com. ghj. packageofservlet;
- Import java. io. IOException;
- Import java. io. PrintWriter;
- Import javax. servlet. ServletException;
- Import javax. servlet. http. HttpServlet;
- Import javax. servlet. http. HttpServletRequest;
- Import javax. servlet. http. HttpServletResponse;
- Public class AndroidServerServlet extends HttpServlet {
- Private static final long serialVersionUID = 6792316567928634227l;
- Public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- Response. setContentType ("text/plain; charset = UTF-8 ");
- Request. setCharacterEncoding ("UTF-8 ");
- System. err. println (request. getParameter ("clientData "));
- PrintWriter printWriter = response. getWriter ();
- PrintWriter. print ("Hello Android client! ");
- PrintWriter. flush ();
- PrintWriter. close ();
- }
- }
Code 2: modify a file named "web. xml"
[Html]View plain copy
- <? Xml version = "1.0" encoding = "UTF-8"?>
- <Web-app version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee"
- Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
- Xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee
- Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd>
- <Servlet>
- <Servlet-name> AndroidServerServlet </servlet-name>
- <Servlet-class> com. ghj. packageofservlet. AndroidServerServlet </servlet-class>
- </Servlet>
- <Servlet-mapping>
- <Servlet-name> AndroidServerServlet </servlet-name>
- <Url-pattern>/AndroidServerServlet </url-pattern>
- </Servlet-mapping>
- </Web-app>
Ii. Android mobile client:
Code 1: Download the jar file named android-async-http-1.4.6.jar,: http://download.csdn.net/download/wangshuxuncom/8116169
Code 2: Add a file named "AndroidClientActivity. java"
[Java]View plain copy
- Package com. example. androidclient;
- Import org. apache. http. Header;
- Import android. app. Activity;
- Import android. OS. Bundle;
- Import android. view. View;
- Import android. view. View. OnClickListener;
- Import android. widget. Button;
- Import android. widget. Toast;
- Import com. loopj. android. http. AsyncHttpClient;
- Import com. loopj. android. http. AsyncHttpResponseHandler;
- Import com. loopj. android. http. RequestParams;
- Public class AndroidClientActivity extends Activity {
- @ Override
- Protected void onCreate (Bundle savedInstanceState ){
- Super. onCreate (savedInstanceState );
- SetContentView (R. layout. android_client );
- Button sendButton = (Button) findViewById (R. id. send_button );
- SendButton. setOnClickListener (new OnClickListener (){
- @ Override
- Public void onClick (View v ){
- RequestParams requestParams = new RequestParams ();
- RequestParams. add ("clientData", "Hello, server! ");
- New AsyncHttpClient (). post ("http: // 172.16.99.207: 8080/AndroidServer/AndroidServerServlet", requestParams, new AsyncHttpResponseHandler (){
- @ Override
- Public void onSuccess (int statusCode, Header [] headers, byte [] responseBody ){
- If (statusCode = 200 ){
- Toast. makeText (AndroidClientActivity. this, new String (responseBody), Toast. LENGTH_LONG). show ();
- }
- }
- @ Override
- Public void onFailure (int statusCode, Header [] headers, byte [] responseBody, Throwable error ){
- Toast. makeText (AndroidClientActivity. this, "no response is obtained from the Android server! ", Toast. LENGTH_LONG). show ();
- }
- });
- }
- });
- }
- }
Code 3: Add a file named "android_client.xml"
[Html]View plain copy
- <RelativeLayout 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: paddingBottom = "@ dimen/activity_vertical_margin"
- Android: paddingLeft = "@ dimen/activity_horizontal_margin"
- Android: paddingRight = "@ dimen/activity_horizontal_margin"
- Android: paddingTop = "@ dimen/activity_vertical_margin"
- Tools: context = ". MainActivity">
- <Button
- Android: id = "@ + id/send_button"
- Android: layout_width = "wrap_content"
- Android: layout_height = "wrap_content"
- Android: layout_centerHorizontal = "true"
- Android: layout_centerVertical = "true"
- Android: text = "@ string/hello_server"/>
- </RelativeLayout>
English version (using android-async-http-1.4.6.jar jar this form of Android client and server data interaction is the most common.