前言:作為一名android開發(fā)人員坎藐,網(wǎng)絡(luò)數(shù)據(jù)都是web開發(fā)人員提供,每次讓他們寫一個接口都跟求神拜佛一樣赋续,與其求別人還不如自己動手.......(千萬只羊駝從天空飛過)總算研究出來了,寫下來有的人可能會用到。也算是自己做個筆記罐农。
項目使用MyEclipse+Struts+Tomcat搭建的 ,這三個從網(wǎng)站下載即可催什,Tomcat需要配置涵亏,網(wǎng)上這類的文章很多,這里不再介紹蒲凶。
配置Struts
下載下來后气筋,解壓,找到這兩個目錄
MyEclipse新建工程
File->New->WebProject 選擇Next即可旋圆,記得把這個勾上(減少麻煩)
新建完工程后宠默,把剛才復(fù)制的兩個分別放到這兩個地方,對應(yīng)放進(jìn)去(struts.xml有錯誤灵巧,等會再修改配置光稼,不用著急)
新建兩個包,UserAction和User兩個類
public class UserAction extends ActionSupport{
private String username;
private String password;
private HttpServletResponse response;
private User model;
public String login() throws Exception{
System.out.println(username+","+password);
response=ServletActionContext.getResponse();
response.setContentType("application/json");
Gson gson = new Gson();
String str = gson.toJson(getContent());
PrintWriter writer=response.getWriter();
writer.write(str);
return null;
}
private User getContent(){
model=new User();
model.setName("Android");
model.setAge("100");
model.setSex("Male");
return model;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
public class User {
private String name;
private String sex;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
下面修改Struts.xml里面的錯誤
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="login" class="com.server.action.UserAction" method="login"></action>
</package>
//name 隨便起
//class 對應(yīng)的包名+類一定要對應(yīng)起來(按住ctrl點擊看看能不能跳轉(zhuǎn)過去孩等,判斷寫的對不對)
//method login對應(yīng)的是UserAction里的login()方法
</struts>
最后修改WebRoot->WEB-INF-web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>myServer</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
添加filter和filter-mapping配置struts2
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
最后艾君,啟動Tomcat,如果這個沒有Servers這個欄目肄方,點擊Window->ShowView->Servers添加
右鍵啟動Tomcat
Android端來解析
OkHttpClient mOkhttpClient = new OkHttpClient();
final Request request = new Request.Builder().url(URL + "login?username=wangwei&password=666666").build();
Call call = mOkhttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
String res = response.body().string();
Log.e("tag", "return ===" + res);
try {
JSONObject jsonObject=new JSONObject(res);
String name=jsonObject.optString("name");
String sex=jsonObject.optString("sex");
String age=jsonObject.optString("age");
Log.e("tag","name==="+name+",age==="+age+",sex==="+sex);
} catch (JSONException e) {
e.printStackTrace();
}
}
});