android端和Struts2服务器端通信,交互信息,参数采用JSON,使用了HttpClient与HttpPost类
2014-10-29 03:04
519 查看
首先是Struts端的程序,采用Struts2.1.6
1:web.xml的配置,主要是配置Struts2的filter
[html] view plaincopyprint?
<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>
2:struts.xml的内容:
[html] view plaincopyprint?
<package name="testjson" extends="json-default">
<action name="getjson" class="com.capinfotech.json.JSONAction" method="json">
<result type="json" />
</action>
</package>
3:JSONAciton的内容为:
[html] view plaincopyprint?
package com.capinfotech.json;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import com.opensymphony.xwork2.ActionSupport;
public class JSONAction extends ActionSupport implements ServletRequestAware, ServletResponseAware{
private static final long serialVersionUID = -989477296829078690L;
private HttpServletRequest request;
private HttpServletResponse response;
private String format;
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
public void json() {
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", 1);
jsonObject.put("title", "哈利波特");
jsonObject.put("timelength", 89);
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("id", 2);
jsonObject1.put("title", "速度与激情");
jsonObject1.put("timelength", 120);
JSONObject jsonObject2 = new JSONObject();
jsonObject2.put("id", 3);
jsonObject2.put("title", "变形金刚3");
jsonObject2.put("timelength", 100);
jsonArray.add(0, jsonObject);
jsonArray.add(1, jsonObject1);
jsonArray.add(2, jsonObject2);
try {
this.response.setCharacterEncoding("UTF-8");
this.response.getWriter().write(jsonArray.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}
4:运行效果为如下图:
![](https://oscdn.geek-share.com/Uploads/Images/Content/201911/10/0b582e3f23f6f4dd2a2bf68ee7b2246a.gif)
5:运行注意事项,当Struts2中使用JSON时,一定要添加够JSON使用的包,否则会出错,要添加的包如下:
ezmorph-1.0.6.jar
commons-lang 2.4
commons-beanutils 1.7.0
commons-collections 3.2
commons-logging 1.1.1
Android端的程序和配置
1:androidmanifest.xml的内容为:
[html] view
plaincopyprint?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.capinfotech.json"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
2:main.xml的内容主要是定义了ListView用来显示最新的电影咨询
[html] view
plaincopyprint?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@+id/videos"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
3:item.xml的内容,主要用来定义ListView里每个元素的显示方式
[html] view plaincopyprint?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="250dip"
android:layout_height="wrap_content"
android:id="@+id/title"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/timelength"
/>
</LinearLayout>
4:定义了一个实体类Video
[java] view plaincopyprint?
package com.capinfotech.model;
public class Video {
private Integer id;
private String name;
private Integer time;
public Video() {
}
public Video(Integer id, String name, Integer time) {
super();
this.id = id;
this.name = name;
this.time = time;
}
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 Integer getTime() {
return time;
}
public void setTime(Integer time) {
this.time = time;
}
}
5:MainActivity的内容
[java] view plaincopyprint?
package cn.capinfotech.json;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private List<HashMap<String, Object>> videos = null;
private HashMap<String, Object> video = null;
private ListView listView = null;
private static String url = "http://10.0.2.2:8088/Struts2_sxt/getjson.action";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView)findViewById(R.id.videos);
getPDAServerData(url);
}
private void getPDAServerData(String url) {
HttpClient client = new DefaultHttpClient();
//提拱默认的HttpClient实现
HttpPost request;
try {
request = new HttpPost(new URI(url));
HttpResponse response = client.execute(request);
// 判断请求是否成功
if (response.getStatusLine().getStatusCode() == 200) { //200表示请求成功
HttpEntity entity = response.getEntity();
if (entity != null) {
String out = EntityUtils.toString(entity, "UTF-8");
Log.i(TAG, out);
JSONArray jsonArray = new JSONArray(out);
videos = new ArrayList<HashMap<String, Object>>();
for(int i = 0; i<jsonArray.length(); i++) {
JSONObject jsonObject = (JSONObject) jsonArray.get(i);
int id = jsonObject.getInt("id");
String name = jsonObject.getString("title");
int timelength = jsonObject.getInt("timelength");
video = new HashMap<String, Object>();
video.put("id", id);
video.put("name", name);
video.put("timelength", "时长为:" + timelength);
videos.add(video);
}
SimpleAdapter adapter = new SimpleAdapter(this, videos, R.layout.item,
new String[]{"name", "timelength"},
new int[]{R.id.title, R.id.timelength}
);
listView.setAdapter(adapter);
}
}
} catch(Exception e) {
e.printStackTrace();
Log.e(TAG, e.toString());
Toast.makeText(MainActivity.this, "获取数据失败", Toast.LENGTH_LONG).show();
}
}
}
6:程序界面效果图
1:web.xml的配置,主要是配置Struts2的filter
[html] view plaincopyprint?
<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>
<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>
2:struts.xml的内容:
[html] view plaincopyprint?
<package name="testjson" extends="json-default">
<action name="getjson" class="com.capinfotech.json.JSONAction" method="json">
<result type="json" />
</action>
</package>
<package name="testjson" extends="json-default"> <action name="getjson" class="com.capinfotech.json.JSONAction" method="json"> <result type="json" /> </action> </package>
3:JSONAciton的内容为:
[html] view plaincopyprint?
package com.capinfotech.json;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import com.opensymphony.xwork2.ActionSupport;
public class JSONAction extends ActionSupport implements ServletRequestAware, ServletResponseAware{
private static final long serialVersionUID = -989477296829078690L;
private HttpServletRequest request;
private HttpServletResponse response;
private String format;
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
public void json() {
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", 1);
jsonObject.put("title", "哈利波特");
jsonObject.put("timelength", 89);
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("id", 2);
jsonObject1.put("title", "速度与激情");
jsonObject1.put("timelength", 120);
JSONObject jsonObject2 = new JSONObject();
jsonObject2.put("id", 3);
jsonObject2.put("title", "变形金刚3");
jsonObject2.put("timelength", 100);
jsonArray.add(0, jsonObject);
jsonArray.add(1, jsonObject1);
jsonArray.add(2, jsonObject2);
try {
this.response.setCharacterEncoding("UTF-8");
this.response.getWriter().write(jsonArray.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.capinfotech.json; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.apache.struts2.interceptor.ServletRequestAware; import org.apache.struts2.interceptor.ServletResponseAware; import com.opensymphony.xwork2.ActionSupport; public class JSONAction extends ActionSupport implements ServletRequestAware, ServletResponseAware{ private static final long serialVersionUID = -989477296829078690L; private HttpServletRequest request; private HttpServletResponse response; private String format; public String getFormat() { return format; } public void setFormat(String format) { this.format = format; } public void setServletRequest(HttpServletRequest request) { this.request = request; } public void setServletResponse(HttpServletResponse response) { this.response = response; } public void json() { JSONArray jsonArray = new JSONArray(); JSONObject jsonObject = new JSONObject(); jsonObject.put("id", 1); jsonObject.put("title", "哈利波特"); jsonObject.put("timelength", 89); JSONObject jsonObject1 = new JSONObject(); jsonObject1.put("id", 2); jsonObject1.put("title", "速度与激情"); jsonObject1.put("timelength", 120); JSONObject jsonObject2 = new JSONObject(); jsonObject2.put("id", 3); jsonObject2.put("title", "变形金刚3"); jsonObject2.put("timelength", 100); jsonArray.add(0, jsonObject); jsonArray.add(1, jsonObject1); jsonArray.add(2, jsonObject2); try { this.response.setCharacterEncoding("UTF-8"); this.response.getWriter().write(jsonArray.toString()); } catch (IOException e) { e.printStackTrace(); } } }
4:运行效果为如下图:
![](https://oscdn.geek-share.com/Uploads/Images/Content/201911/10/0b582e3f23f6f4dd2a2bf68ee7b2246a.gif)
5:运行注意事项,当Struts2中使用JSON时,一定要添加够JSON使用的包,否则会出错,要添加的包如下:
ezmorph-1.0.6.jar
commons-lang 2.4
commons-beanutils 1.7.0
commons-collections 3.2
commons-logging 1.1.1
Android端的程序和配置
1:androidmanifest.xml的内容为:
[html] view
plaincopyprint?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.capinfotech.json"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.capinfotech.json" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="8" /> <uses-permission android:name="android.permission.INTERNET" /> </manifest>
2:main.xml的内容主要是定义了ListView用来显示最新的电影咨询
[html] view
plaincopyprint?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@+id/videos"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android:id="@+id/videos" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
3:item.xml的内容,主要用来定义ListView里每个元素的显示方式
[html] view plaincopyprint?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="250dip"
android:layout_height="wrap_content"
android:id="@+id/title"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/timelength"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="250dip" android:layout_height="wrap_content" android:id="@+id/title" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/timelength" /> </LinearLayout>
4:定义了一个实体类Video
[java] view plaincopyprint?
package com.capinfotech.model;
public class Video {
private Integer id;
private String name;
private Integer time;
public Video() {
}
public Video(Integer id, String name, Integer time) {
super();
this.id = id;
this.name = name;
this.time = time;
}
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 Integer getTime() {
return time;
}
public void setTime(Integer time) {
this.time = time;
}
}
package com.capinfotech.model; public class Video { private Integer id; private String name; private Integer time; public Video() { } public Video(Integer id, String name, Integer time) { super(); this.id = id; this.name = name; this.time = time; } 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 Integer getTime() { return time; } public void setTime(Integer time) { this.time = time; } }
5:MainActivity的内容
[java] view plaincopyprint?
package cn.capinfotech.json;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private List<HashMap<String, Object>> videos = null;
private HashMap<String, Object> video = null;
private ListView listView = null;
private static String url = "http://10.0.2.2:8088/Struts2_sxt/getjson.action";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView)findViewById(R.id.videos);
getPDAServerData(url);
}
private void getPDAServerData(String url) {
HttpClient client = new DefaultHttpClient();
//提拱默认的HttpClient实现
HttpPost request;
try {
request = new HttpPost(new URI(url));
HttpResponse response = client.execute(request);
// 判断请求是否成功
if (response.getStatusLine().getStatusCode() == 200) { //200表示请求成功
HttpEntity entity = response.getEntity();
if (entity != null) {
String out = EntityUtils.toString(entity, "UTF-8");
Log.i(TAG, out);
JSONArray jsonArray = new JSONArray(out);
videos = new ArrayList<HashMap<String, Object>>();
for(int i = 0; i<jsonArray.length(); i++) {
JSONObject jsonObject = (JSONObject) jsonArray.get(i);
int id = jsonObject.getInt("id");
String name = jsonObject.getString("title");
int timelength = jsonObject.getInt("timelength");
video = new HashMap<String, Object>();
video.put("id", id);
video.put("name", name);
video.put("timelength", "时长为:" + timelength);
videos.add(video);
}
SimpleAdapter adapter = new SimpleAdapter(this, videos, R.layout.item,
new String[]{"name", "timelength"},
new int[]{R.id.title, R.id.timelength}
);
listView.setAdapter(adapter);
}
}
} catch(Exception e) {
e.printStackTrace();
Log.e(TAG, e.toString());
Toast.makeText(MainActivity.this, "获取数据失败", Toast.LENGTH_LONG).show();
}
}
}
package cn.capinfotech.json; import java.net.URI; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import org.json.JSONArray; import org.json.JSONObject; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.Toast; public class MainActivity extends Activity { private static final String TAG = "MainActivity"; private List<HashMap<String, Object>> videos = null; private HashMap<String, Object> video = null; private ListView listView = null; private static String url = "http://10.0.2.2:8088/Struts2_sxt/getjson.action"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); listView = (ListView)findViewById(R.id.videos); getPDAServerData(url); } private void getPDAServerData(String url) { HttpClient client = new DefaultHttpClient(); //提拱默认的HttpClient实现 HttpPost request; try { request = new HttpPost(new URI(url)); HttpResponse response = client.execute(request); // 判断请求是否成功 if (response.getStatusLine().getStatusCode() == 200) { //200表示请求成功 HttpEntity entity = response.getEntity(); if (entity != null) { String out = EntityUtils.toString(entity, "UTF-8"); Log.i(TAG, out); JSONArray jsonArray = new JSONArray(out); videos = new ArrayList<HashMap<String, Object>>(); for(int i = 0; i<jsonArray.length(); i++) { JSONObject jsonObject = (JSONObject) jsonArray.get(i); int id = jsonObject.getInt("id"); String name = jsonObject.getString("title"); int timelength = jsonObject.getInt("timelength"); video = new HashMap<String, Object>(); video.put("id", id); video.put("name", name); video.put("timelength", "时长为:" + timelength); videos.add(video); } SimpleAdapter adapter = new SimpleAdapter(this, videos, R.layout.item, new String[]{"name", "timelength"}, new int[]{R.id.title, R.id.timelength} ); listView.setAdapter(adapter); } } } catch(Exception e) { e.printStackTrace(); Log.e(TAG, e.toString()); Toast.makeText(MainActivity.this, "获取数据失败", Toast.LENGTH_LONG).show(); } } }
6:程序界面效果图
![](https://oscdn.geek-share.com/Uploads/Images/Content/201911/10/bfa69e0c692a40448491265e0bbb4943.gif)
相关文章推荐
- android端和Struts2服务器端通信,交互信息,参数采用JSON,使用了HttpClient与HttpPost类
- android端和Struts2服务器端通信,交互信息,参数采用JSON,使用了HttpClient与HttpPost类
- android端和Struts2服务器端通信,交互信息,参数采用JSON,使用了HttpClient与HttpPost类
- 采用JSON交互信息的android端和Struts2服务器端通信,使用了HttpClient与HttpPos
- Android中基于HTTP的通信技术(3)使用HttpClient进行Get方式通信
- Android——使用HttpClient的HttpGet和Post方式进行网络通信
- Java基础知识强化之网络编程笔记18:Android网络通信之 使用HttpClient的Post / Get 方式读取网络数据(基于HTTP通信技术)
- Android中基于HTTP的通信技术(4)使用HttpClient进行Post方式通信
- HTTP基础与Android之(安卓与服务器通信)——使用HttpClient和HttpURLConnection
- Android的与服务器端传送信息使用JSON类-JSONObject与JSONArray的使用
- Android中使用Json和Xml与服务器进行通信,使用代码发送Get和Post请求,http请求辅助类
- android HttpURLConnection 与服务器通信(包含对struts2的使用)
- android菜鸟学习笔记25----与服务器端交互(二)解析服务端返回的json数据及使用一个开源组件请求服务端数据
- Android中使用HTTP和HttpClient进行通信
- Android程序:使用Http的Post方式与网络交互通信
- android菜鸟学习笔记24----与服务器端交互(一)使用HttpURLConnection和HttpClient请求服务端数据
- [Android开发]Android之使用HTTP的get,post,HttpClient三种方式向服务器端提交文本数据
- 一个完整的通过encoding/json信息传递格式,采用http.POST利用进行与服务器通信,并对返回结果进行解析处理的典型案例
- Android中使用HttpClient实现HTTP通信效果
- android 客户端与服务器端之间采用JSON进行数据通信之一