Android后端服務(wù)器的搭建方法
一直做Android前端,今天突然心血來潮想搭建一個后臺玩玩。平時都是需要什么樣的接口直接出個接口文檔扔給后臺的兄弟,自己從來不操心他們內(nèi)部的實(shí)現(xiàn)問題。今天懷著好奇的心理去搭建了一個JAVA編譯環(huán)境下的后臺服務(wù)器。聽說用PHP搭建服務(wù)器的居多,但是我們做大Android的最熟悉的還是Java了,所以下面我就開始搭建這個服務(wù)器。很簡單。。。
首先我下載了一個myelipse應(yīng)為我們開發(fā)android的eclipse不能創(chuàng)建web project 要不然你去下載個插件也行,下載好以后創(chuàng)建web project會生成一個目錄,然后右鍵你的這個項(xiàng)目選擇myeclipse -> add structs capabilities... 選擇2.1 finish OK這樣就創(chuàng)建成功這個項(xiàng)目了,下面我貼出來我的項(xiàng)目樹供大家參考(感謝yayun0516 ,他的博文給了我很大的幫助,但是其中有些不足我已經(jīng)在下面改正了)

下面配置structs.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="struts2" extends="struts-default" namespace="/">
<action name="getjson" class="com.shao.action.JSONAction"
method="json">
<result name="success">index.jsp</result>
</action>
</package>
</struts>
只有這一個需要配置,其他的在你添加struct的時候就會自動生成。下面創(chuàng)建類型文件
package com.shao.domain;
public class Music {
private Integer id;
private String name;
private String time;
private String author;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
然后再創(chuàng)建轉(zhuǎn)json的方法JSONAction:
package com.shao.action;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionSupport;
import com.shao.domain.Music;
public class JSONAction extends ActionSupport implements ServletRequestAware,
ServletResponseAware {
/**
*
*/
private static final long serialVersionUID = -3604892179657815531L;
private HttpServletRequest request;
private HttpServletResponse response;
private String format;
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
}
@Override
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
@Override
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
public void json() {
List<Music> list = new ArrayList<Music>();
Gson gson = new Gson();
Music m1 = new Music();
m1.setId(1);
m1.setAuthor("周");
m1.setName("外婆");
m1.setTime("04:04");
list.add(m1);
Music m2 = new Music();
m2.setId(2);
m2.setAuthor("周杰倫");
m2.setName("半獸人");
m2.setTime("04:05");
list.add(m2);
Music m3 = new Music();
m3.setId(3);
m3.setAuthor("周杰倫");
m3.setName("烏克麗麗");
m3.setTime("02:55");
list.add(m3);
java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<List<Music>>() {
}.getType(); // 指定type
String beanListToJson = gson.toJson(list, type); // list轉(zhuǎn)換成json字符串
System.out.println("GSON-->" + beanListToJson);
try {
response.setContentType("application/json; charset=GBK");
response.setCharacterEncoding("UTF-8");
this.response.getWriter().write(beanListToJson);
} catch (IOException e) {
e.printStackTrace();
}
}
}
response.setContentType("application/json; charset=GBK");一定要注意,如果不加這句會在你請求服務(wù)器數(shù)據(jù)的時候,中文出現(xiàn)亂碼現(xiàn)象,同時在index.jsp中加入了contentType="text/html; charset=GBK"
還有不要忘了導(dǎo)入Gson包。
完了,就這樣服務(wù)器就完成了,下面運(yùn)行一下 run as -> myeclipse service application 成功后會彈出一個框,上面寫著This is my JSP page.這就說明你已經(jīng)成功創(chuàng)建了服務(wù)器。
下面打開http://localhost:8080/Test2/getjson.action 下面就是服務(wù)器返回的內(nèi)容了。
基本就是這樣了,又不懂的可以問我。下面說android端的,更簡單了。
創(chuàng)建我們的項(xiàng)目然后加入xutils和gson包。

這是一個新建的項(xiàng)目,在activity_main.xml中我給那個TextView添加了一個id
然后在MainActivity中實(shí)現(xiàn)如下:
package com.example.test2;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView text = (TextView) findViewById(R.id.text);
HttpUtils httpUtils = new HttpUtils();
httpUtils.send(HttpMethod.POST, "http://192.168.199.171:8080/Test2/getjson.action", new RequestCallBack<String>() {
public void onFailure(HttpException arg0, String arg1) {
Log.d("=====================onFailure", arg1+";"+arg0.toString());
}
public void onSuccess(ResponseInfo<String> arg0) {
Log.d("=====================onSuccess", arg0.result);
text.setText(arg0.result);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
整個android端就是這樣了,下面我們運(yùn)行一下剛才的數(shù)據(jù)已經(jīng)顯示在了該TextView上。
其實(shí)整個代碼內(nèi)容是很簡單的,主要難的地方就是在環(huán)境搭建上,大家多練練吧,整個代碼是我跑下來的,所以代碼沒有問題,如果你跑不成功就多去研究研究環(huán)境搭建。
分享至此,以后可以往這方面多了解一下,就算不做后臺開發(fā),也要多了解了解,減少溝通成本。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android客戶端從服務(wù)器端獲取json數(shù)據(jù)并解析的實(shí)現(xiàn)代碼
- Android使用post方式上傳圖片到服務(wù)器的方法
- Android客戶端post請求服務(wù)器端實(shí)例
- android 上傳文件到服務(wù)器代碼實(shí)例
- Android編程之客戶端通過socket與服務(wù)器通信的方法
- android異步請求服務(wù)器數(shù)據(jù)示例
- Android編程向服務(wù)器發(fā)送請求時出現(xiàn)中文亂碼問題的解決方法
- python服務(wù)器與android客戶端socket通信實(shí)例
- Android TCP 文件客戶端與服務(wù)器DEMO介紹
- Android SDK在線更新鏡像服務(wù)器大全
相關(guān)文章
詳解Flutter Image組件如何處理圖片加載過程中的錯誤
在Flutter中,Image組件可以通過監(jiān)聽加載過程中的錯誤來處理圖片加載過程中的錯誤,本文小編將給大家詳細(xì)介紹了Flutter Image組件是如何處理圖片加載過程中的錯誤,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參下2023-10-10
Android編程之菜單Menu的創(chuàng)建方法示例
這篇文章主要介紹了Android編程之菜單Menu的創(chuàng)建方法,結(jié)合實(shí)例形式分析了Android菜單Menu的布局、響應(yīng)及功能實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-08-08
Kotlin 使用Lambda來設(shè)置回調(diào)的操作
這篇文章主要介紹了Kotlin 使用Lambda來設(shè)置回調(diào)的操作方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
詳解 android 光線傳感器 light sensor的使用
這篇文章主要介紹了詳解 android 光線傳感器 light sensor的使用的相關(guān)資料,需要的朋友可以參考下2017-06-06
Android無障礙監(jiān)聽通知的實(shí)戰(zhàn)過程
開發(fā)微動手勢的時候,做了一個通知觸發(fā)的功能,就是在收到某個預(yù)設(shè)的通知的時候,自動觸發(fā)某個動作,因此需要監(jiān)聽通知消息,這篇文章主要給大家介紹了關(guān)于Android無障礙監(jiān)聽通知的相關(guān)資料,需要的朋友可以參考下2022-07-07
Android頭像上傳功能的實(shí)現(xiàn)代碼(獲取頭像加剪切)
最近在做一個頭像上傳的項(xiàng)目,下面小編給大家分享Android頭像上傳功能的實(shí)現(xiàn)代碼,需要的的朋友參考下吧2017-08-08

