前言:作为一名android开发人员,网络数据都是web开发人员提供,每次让他们写一个接口都跟求神拜佛一样,与其求别人还不如自己动手.......(千万只羊驼从天空飞过)总算研究出来了,写下来有的人可能会用到。也算是自己做个笔记。
项目使用MyEclipse+Struts+Tomcat搭建的 ,这三个从网站下载即可,Tomcat需要配置,网上这类的文章很多,这里不再介绍。
配置Struts
下载下来后,解压,找到这两个目录
MyEclipse新建工程
File->New->WebProject 选择Next即可,记得把这个勾上(减少麻烦)
新建完工程后,把刚才复制的两个分别放到这两个地方,对应放进去(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 对应的包名+类一定要对应起来(按住ctrl点击看看能不能跳转过去,判断写的对不对)
//method login对应的是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();
}
}
});