首先是Struts端的程序,采用Struts2.1.6
1:web.xml的配置,主要是配置Struts2的filter
<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的内容:
<package name="testjson" extends="json-default"> <action name="getjson" class="com.capinfotech.json.JSONAction" method="json"> <result type="json" /> </action> </package>
3:JSONAciton的内容为:
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:运行效果为如下图:
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的内容为:
<?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用来显示最新的电影咨询
<?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里每个元素的显示方式
<?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
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的内容
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:程序界面效果图