Android後端伺服器的搭建方法

來源:互聯網
上載者:User

標籤:public   後端服務   服務   int   ken   匯入   說明   tput   ack   

一直做Android前端,今天突然心血來潮想搭建一個後台玩玩。平時都是需要什麼樣的介面直接出個介面文檔扔給背景兄弟,自己從來不操心他們內部的實現問題。今天懷著好奇的心理去搭建了一個JAVA編譯環境下的後台伺服器。聽說用PHP搭建伺服器的居多,但是我們做大Android的最熟悉的還是Java了,所以下面我就開始搭建這個伺服器。很簡單。。。

首先我下載了一個myelipse應為我們開發android的eclipse不能建立web project 要不然你去下載個外掛程式也行,下載好以後建立web project會產生一個目錄,然後右鍵你的這個項目選擇myeclipse -> add structs capabilities... 選擇2.1 finish OK這樣就建立成功這個項目了,下面我貼出來我的項目樹供大家參考(感謝yayun0516 ,他的博文給了我很大的協助,但是其中有些不足我已經在下面改正了)

下面配置structs.xml

  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>
複製代碼

只有這一個需要配置,其他的在你添加struct的時候就會自動產生。下面建立類型檔案

  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. }
複製代碼

然後再建立轉json的方法JSONAction:

  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("周");
  43. m1.setName("外婆");
  44. m1.setTime("04:04");
  45. list.add(m1);
  46. Music m2 = new Music();
  47. m2.setId(2);
  48. m2.setAuthor("周杰倫");
  49. m2.setName("半獸人");
  50. m2.setTime("04:05");
  51. list.add(m2);
  52. Music m3 = new Music();
  53. m3.setId(3);
  54. m3.setAuthor("周杰倫");
  55. m3.setName("烏克麗麗");
  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(); // 指定type
  60. String beanListToJson = gson.toJson(list, type); // list轉換成json字串
  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. }
複製代碼

response.setContentType("application/json; charset=GBK");一定要注意,如果不加這句會在你請求伺服器資料的時候,中文出現亂碼現象,同時在index.jsp中加入了contentType="text/html; charset=GBK"
還有不要忘了匯入Gson包。

完了,就這樣伺服器就完成了,下面運行一下 run as -> myeclipse service application 成功後會彈出一個框,上面寫著This is my JSP page.這就說明你已經成功建立了伺服器。

下面開啟http://localhost:8080/Test2/getjson.action 下面就是伺服器返回的內容了。

基本就是這樣了,又不懂的可以問我。下面說android端的,更簡單了。

建立我們的項目然後加入xutils和gson包。

這是一個建立的項目,在activity_main.xml中我給那個TextView添加了一個id

然後在MainActivity中實現如下:

  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 will
  40. // automatically handle clicks on the Home/Up button, so long
  41. // as you specify a 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. }
複製代碼

整個android端就是這樣了,下面我們運行一下剛才的資料已經顯示在了該TextView上。

其實整個代碼內容是很簡單的,主要難的地方就是在環境搭建上,大家多練練吧,整個代碼是我跑下來的,所以代碼沒有問題,如果你跑不成功就多去研究研究環境搭建。

分享至此,以後可以往這方面多瞭解一下,就算不做後台開發,也要多瞭解瞭解,減少溝通成本。

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/

Android後端伺服器的搭建方法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.