Android client and server data interaction, android Server

Source: Internet
Author: User

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
  1. Package com. ghj. packageofservlet;
  2. Import java. io. IOException;
  3. Import java. io. PrintWriter;
  4. Import javax. servlet. ServletException;
  5. Import javax. servlet. http. HttpServlet;
  6. Import javax. servlet. http. HttpServletRequest;
  7. Import javax. servlet. http. HttpServletResponse;
  8. Public class AndroidServerServlet extends HttpServlet {
  9. Private static final long serialVersionUID = 6792316567928634227l;
  10. Public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  11. Response. setContentType ("text/plain; charset = UTF-8 ");
  12. Request. setCharacterEncoding ("UTF-8 ");
  13. System. err. println (request. getParameter ("clientData "));
  14. PrintWriter printWriter = response. getWriter ();
  15. PrintWriter. print ("Hello Android client! ");
  16. PrintWriter. flush ();
  17. PrintWriter. close ();
  18. }
  19. }

Code 2: modify a file named "web. xml"

[Html]View plain copy
  1. <? Xml version = "1.0" encoding = "UTF-8"?>
  2. <Web-app version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee"
  3. Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
  4. Xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee
  5. Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd>
  6. <Servlet>
  7. <Servlet-name> AndroidServerServlet </servlet-name>
  8. <Servlet-class> com. ghj. packageofservlet. AndroidServerServlet </servlet-class>
  9. </Servlet>
  10. <Servlet-mapping>
  11. <Servlet-name> AndroidServerServlet </servlet-name>
  12. <Url-pattern>/AndroidServerServlet </url-pattern>
  13. </Servlet-mapping>
  14. </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
  1. Package com. example. androidclient;
  2. Import org. apache. http. Header;
  3. Import android. app. Activity;
  4. Import android. OS. Bundle;
  5. Import android. view. View;
  6. Import android. view. View. OnClickListener;
  7. Import android. widget. Button;
  8. Import android. widget. Toast;
  9. Import com. loopj. android. http. AsyncHttpClient;
  10. Import com. loopj. android. http. AsyncHttpResponseHandler;
  11. Import com. loopj. android. http. RequestParams;
  12. Public class AndroidClientActivity extends Activity {
  13. @ Override
  14. Protected void onCreate (Bundle savedInstanceState ){
  15. Super. onCreate (savedInstanceState );
  16. SetContentView (R. layout. android_client );
  17. Button sendButton = (Button) findViewById (R. id. send_button );
  18. SendButton. setOnClickListener (new OnClickListener (){
  19. @ Override
  20. Public void onClick (View v ){
  21. RequestParams requestParams = new RequestParams ();
  22. RequestParams. add ("clientData", "Hello, server! ");
  23. New AsyncHttpClient (). post ("http: // 172.16.99.207: 8080/AndroidServer/AndroidServerServlet", requestParams, new AsyncHttpResponseHandler (){
  24. @ Override
  25. Public void onSuccess (int statusCode, Header [] headers, byte [] responseBody ){
  26. If (statusCode = 200 ){
  27. Toast. makeText (AndroidClientActivity. this, new String (responseBody), Toast. LENGTH_LONG). show ();
  28. }
  29. }
  30. @ Override
  31. Public void onFailure (int statusCode, Header [] headers, byte [] responseBody, Throwable error ){
  32. Toast. makeText (AndroidClientActivity. this, "no response is obtained from the Android server! ", Toast. LENGTH_LONG). show ();
  33. }
  34. });
  35. }
  36. });
  37. }
  38. }

Code 3: Add a file named "android_client.xml"

[Html]View plain copy
  1. <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
  2. Xmlns: tools = "http://schemas.android.com/tools"
  3. Android: layout_width = "match_parent"
  4. Android: layout_height = "match_parent"
  5. Android: paddingBottom = "@ dimen/activity_vertical_margin"
  6. Android: paddingLeft = "@ dimen/activity_horizontal_margin"
  7. Android: paddingRight = "@ dimen/activity_horizontal_margin"
  8. Android: paddingTop = "@ dimen/activity_vertical_margin"
  9. Tools: context = ". MainActivity">
  10. <Button
  11. Android: id = "@ + id/send_button"
  12. Android: layout_width = "wrap_content"
  13. Android: layout_height = "wrap_content"
  14. Android: layout_centerHorizontal = "true"
  15. Android: layout_centerVertical = "true"
  16. Android: text = "@ string/hello_server"/>
  17. </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.

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.