How to build an Android back-end server

Source: Internet
Author: User

Always do Android front-end, today suddenly whim want to build a backstage play. Usually need what kind of interface directly out of the interface documents thrown to the back of the brothers, they never worry about their internal implementation problems. Today, with a curious mind to build a Java compiler environment in the background server. I heard that using PHP to build the majority of servers, but we are big Android is the most familiar or Java, so I began to build this server. Very simple...

First I downloaded a myelipse should be for us to develop Android Eclipse cannot create Web project otherwise you go to download a plugin also line, download good later create Web project will generate a directory, then right click on your project to choose MyEclipse --Add structs capabilities ... Select 2.1 Finish OK this will create a successful project, the following I put out my project tree for your reference (thanks to yayun0516, his blog has given me a lot of help, but there are some deficiencies I have been corrected below)

Configure Structs.xml below

    1. <?xml version= "1.0" encoding= "UTF-8"?>
    2. <! DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.1//en" "http://struts.apache.org/dtds/ Struts-2.1.dtd ">
    3. <struts>
    4. <package name= "struts2" extends= "Struts-default" namespace= "/" >
    5. <action name= "Getjson" class= "Com.shao.action.JSONAction"
    6. method= "JSON" >
    7. <result name= "Success" >index.jsp</result>
    8. </action>
    9. </package>
    10. </struts>
Copy Code

Only this one needs to be configured, and the others will be generated automatically when you add a struct. Create type file below

    1. Package com.shao.domain;
    2. public class Music {
    3. Private Integer ID;
    4. private String name;
    5. Private String time;
    6. Private String author;
    7. Public Integer getId () {
    8. return ID;
    9. }
    10. public void SetId (Integer id) {
    11. This.id = ID;
    12. }
    13. Public String GetName () {
    14. return name;
    15. }
    16. public void SetName (String name) {
    17. THIS.name = name;
    18. }
    19. Public String GetTime () {
    20. return time;
    21. }
    22. public void SetTime (String time) {
    23. This.time = time;
    24. }
    25. Public String Getauthor () {
    26. return author;
    27. }
    28. public void Setauthor (String author) {
    29. This.author = author;
    30. }
    31. }
Copy Code

Then create a method to jsonaction the JSON:

  1. Package com.shao.action;
  2. Import java.io.IOException;
  3. Import java.io.UnsupportedEncodingException;
  4. Import Java.net.URLDecoder;
  5. Import java.util.ArrayList;
  6. Import java.util.List;
  7. Import Javax.servlet.http.HttpServletRequest;
  8. Import Javax.servlet.http.HttpServletResponse;
  9. Import Org.apache.struts2.interceptor.ServletRequestAware;
  10. Import Org.apache.struts2.interceptor.ServletResponseAware;
  11. Import Com.google.gson.Gson;
  12. Import Com.opensymphony.xwork2.ActionSupport;
  13. Import Com.shao.domain.Music;
  14. public class Jsonaction extends Actionsupport implements Servletrequestaware,
  15. Servletresponseaware {
  16. /**
  17. *
  18. */
  19. Private static final long serialversionuid = -3604892179657815531l;
  20. private HttpServletRequest request;
  21. Private HttpServletResponse response;
  22. Private String format;
  23. Public String GetFormat () {
  24. return format;
  25. }
  26. public void SetFormat (String format) {
  27. This.format = format;
  28. }
  29. @Override
  30. public void Setservletrequest (HttpServletRequest request) {
  31. This.request = Request;
  32. }
  33. @Override
  34. public void Setservletresponse (HttpServletResponse response) {
  35. This.response = response;
  36. }
  37. public void json () {
  38. list<music> list = new arraylist<music> ();
  39. Gson Gson = new Gson ();
  40. Music M1 = new Music ();
  41. M1.setid (1);
  42. M1.setauthor ("Week");
  43. M1.setname ("grandmother");
  44. M1.settime ("04:04");
  45. List.add (M1);
  46. Music m2 = new Music ();
  47. M2.setid (2);
  48. M2.setauthor ("Jay Chou");
  49. M2.setname ("Orcs");
  50. M2.settime ("04:05");
  51. List.add (m2);
  52. Music m3 = new Music ();
  53. M3.setid (3);
  54. M3.setauthor ("Jay Chou");
  55. M3.setname ("ukulele");
  56. M3.settime ("02:55");
  57. List.add (m3);
  58. Java.lang.reflect.Type Type = new Com.google.gson.reflect.typetoken<list<music>> () {
  59. }.gettype (); Specify type
  60. String Beanlisttojson = Gson.tojson (list, type); List converted to JSON string
  61. System.out.println ("gson-->" + Beanlisttojson);
  62. try {
  63. Response.setcontenttype ("Application/json; CHARSET=GBK ");
  64. Response.setcharacterencoding ("UTF-8");
  65. This.response.getWriter (). write (Beanlisttojson);
  66. } catch (IOException e) {
  67. E.printstacktrace ();
  68. }
  69. }
  70. }
Copy Code

Response.setcontenttype ("Application/json; CHARSET=GBK "); Be sure to note that if you do not add this sentence will be in your request server data, Chinese garbled phenomenon, while in index.jsp added contenttype=" text/html; CHARSET=GBK "
And don't forget to import the Gson package.

That's it, that's it. The server is finished, run as MyEclipse Service application after the success will pop up a box that says this is my JSP page. That means you've successfully created the server.

Below open http://localhost:8080/Test2/getjson.action below is the content returned by the server.

Basically this is the case, and do not understand can ask me. The following is the Android side, more simple.

Create our project and then join the Xutils and Gson packages.

This is a new project, in Activity_main.xml I added an ID to that TextView

It is then implemented in Mainactivity as follows:

  1. Package com.example.test2;
  2. Import Com.lidroid.xutils.HttpUtils;
  3. Import com.lidroid.xutils.exception.HttpException;
  4. Import Com.lidroid.xutils.http.ResponseInfo;
  5. Import Com.lidroid.xutils.http.callback.RequestCallBack;
  6. Import Com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
  7. Import android.support.v7.app.ActionBarActivity;
  8. Import Android.os.Bundle;
  9. Import Android.util.Log;
  10. Import Android.view.Menu;
  11. Import Android.view.MenuItem;
  12. Import Android.view.View;
  13. Import Android.widget.TextView;
  14. public class Mainactivity extends Actionbaractivity {
  15. @Override
  16. protected void OnCreate (Bundle savedinstancestate) {
  17. Super.oncreate (savedinstancestate);
  18. Setcontentview (R.layout.activity_main);
  19. Final TextView Text = (TextView) Findviewbyid (R.id.text);
  20. Httputils httputils = new Httputils ();
  21. Httputils.send (Httpmethod.post, "Http://192.168.199.171:8080/Test2/getjson.action", New requestcallback<string > () {
  22. public void OnFailure (HttpException arg0, String arg1) {
  23. LOG.D ("=====================onfailure", arg1+ ";") +arg0.tostring ());
  24. }
  25. public void onsuccess (responseinfo<string> arg0) {
  26. LOG.D ("=====================onsuccess", Arg0.result);
  27. Text.settext (Arg0.result);
  28. }
  29. });
  30. }
  31. @Override
  32. public boolean Oncreateoptionsmenu (Menu menu) {
  33. Inflate the menu; This adds items to the action bar if it is present.
  34. Getmenuinflater (). Inflate (R.menu.main, menu);
  35. return true;
  36. }
  37. @Override
  38. public boolean onoptionsitemselected (MenuItem item) {
  39. Handle Action Bar Item clicks here. The Action Bar would
  40. Automatically handle clicks on the Home/up button, so long
  41. As you specify a the parent activity in Androidmanifest.xml.
  42. int id = item.getitemid ();
  43. if (id = = r.id.action_settings) {
  44. return true;
  45. }
  46. return super.onoptionsitemselected (item);
  47. }
  48. }
Copy Code

This is the case with the entire Android, let's run it. The data just now appears on the TextView.

In fact the entire code content is very simple, the main difficult place is in the environment to build, we practice it, the entire code is I ran down, so the code is not a problem, if you run not successful on more research environment to build.

Share this, you can learn more about this in the past, even if not to do background development, but also to learn more about, reduce the cost of communication.

http://www.kmjdad.com/
http://www.jnsjzyy.com/
http://www.czhkwl.com/
http://www.express-o2o.com/
http://www.gzjindao.com/
http://www.chumingchuanmeiyishu.com/
http://www.thcxb.com/
http://www.xingguangkeji.com/
http://www.gdrhsy.com/
http://www.clhuiji.com/
http://www.nxjianye.com/
http://www.tjmingsheng.com/
http://www.gangguan022.com/
http://www.zyjbp.com/
http://www.qianhangmy.com/
http://www.tzminbell.com/

How to build an Android back-end server

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.