上节课程我们重点介绍了struts2+json+android服务器段的开发,那这节课程我们就重点介绍在android客户端是怎么解析json集合|实体对象的方式
1、首先在这里我们新建一个android2.2的项目,新建完毕后因为此项目要进行网络访问操作,所以第一步应该在androidMainifest.xml文件中添加网络访问权限代码如下:
<uses-permissionandroid:name="android.permission.INTERNET"></uses-permission>
2、然后创建业务bean及service对应的类代码如下:
业务Bean与上节课程中的业务一致。
Servcie层:
接口:
packagecn.redarmy.service;
importjava.util.List;
importcn.redarmy.domain.Good;
publicinterfaceGoodService{
//获取最新的商品信息
publicabstractList<Good>findAll();
//获取最新的单个商品的详细信息
publicabstractGoodfindById();
}
Service接口的实现类:在这里我们首先完成对商品的详细信息的解析:
packagecn.redarmy.service.Impl;
importjava.io.IOException;
importjava.net.URI;
importjava.net.URISyntaxException;
importjava.util.ArrayList;
importjava.util.List;
importorg.apache.http.HttpEntity;
importorg.apache.http.HttpResponse;
importorg.apache.http.client.ClientProtocolException;
importorg.apache.http.client.HttpClient;
importorg.apache.http.client.methods.HttpGet;
importorg.apache.http.impl.client.DefaultHttpClient;
importorg.apache.http.util.EntityUtils;
importorg.json.JSONArray;
importorg.json.JSONException;
importorg.json.JSONObject;
importcn.redarmy.domain.Good;
importcn.redarmy.service.GoodService;
publicclassGoodServiceImplimplementsGoodService{
//获取最新的商品信息
/*(non-Javadoc)
*@seecn.redarmy.service.Impl.GoodService#findAll()
*/
@Override
publicList<Good>findAll(){
//创建请求HttpClient客户端
HttpClienthttpClient=newDefaultHttpClient();
//创建请求的url
Stringurl="http://192.168.4.32:8080/shop/csdn/listNewsGoods.action";
try{
//创建请求的对象
HttpGetget=newHttpGet(newURI(url));
//发送get请求
HttpResponsehttpResponse=httpClient.execute(get);
//如果服务成功返回响应
if(httpResponse.getStatusLine().getStatusCode()==200){
HttpEntityentity=httpResponse.getEntity();
if(entity!=null){
//获取服务器响应的json字符串
Stringjson=EntityUtils.toString(entity);
returnparseArrayJosn(json);
}
}
}catch(Exceptione){
e.printStackTrace();
}
returnnull;
}
//解析json数组对象
privateList<Good>parseArrayJosn(Stringjson){
List<Good>goods=newArrayList<Good>();
try{
JSONArrayarray=newJSONObject(json).getJSONArray("goods");
for(inti=0;i<array.length();i++){
JSONObjectobj=array.getJSONObject(i);
Goodgood=newGood(obj.getInt("id"),obj.getString("name"),
(float)obj.getDouble("price"));
goods.add(good);
}
}catch(JSONExceptione){
e.printStackTrace();
}
returngoods;
}
//获取最新的单个商品的详细信息
/*(non-Javadoc)
*@seecn.redarmy.service.Impl.GoodService#findById()
*/
@Override
publicGoodfindById(){
//创建请求HttpClient客户端
HttpClienthttpClient=newDefaultHttpClient();
//创建请求的url
Stringurl="http://192.168.4.32:8080/shop/csdn/findGood.action";
try{
//创建请求的对象
HttpGetget=newHttpGet(newURI(url));
//发送get请求
HttpResponsehttpResponse=httpClient.execute(get);
//如果服务成功返回响应
if(httpResponse.getStatusLine().getStatusCode()==200){
HttpEntityentity=httpResponse.getEntity();
if(entity!=null){
//获取服务器响应的json字符串
Stringjson=EntityUtils.toString(entity);
returnparseObjJosn(json);
}
}
}catch(Exceptione){
e.printStackTrace();
}
returnnull;
}
//解析json的单个对象
privateGoodparseObjJosn(Stringjson){
JSONObjectobj=null;
try{
obj=newJSONObject(json).getJSONObject("good");
Goodgood=newGood(obj.getInt("id"),obj.getString("name"),
(float)obj.getDouble("price"));
returngood;
}catch(JSONExceptione){
e.printStackTrace();
}
returnnull;
}
}
创建用于显示商品信息的listView及对应的组件
创建good.xml文件
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"android:orientation="horizontal">
<TextViewandroid:layout_height="wrap_content"android:text="TextView"android:id="@+id/name"android:layout_width="200dip"></TextView>
<TextViewandroid:layout_height="wrap_content"android:text="TextView"android:id="@+id/price"android:layout_width="match_parent"></TextView>
</LinearLayout>
在main.xml文件中添加listView组件
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListViewandroid:layout_height="wrap_content"android:layout_width="match_parent"android:id="@+id/goods"></ListView>
</LinearLayout>
最后完成GoodActivity的编写代码如下:
packagecn.redarmy.activity;
importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.List;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.util.Log;
importandroid.widget.ListView;
importandroid.widget.SimpleAdapter;
importcn.redarmy.domain.Good;
importcn.redarmy.service.GoodService;
importcn.redarmy.service.Impl.GoodServiceImpl;
publicclassGoodActivityextendsActivity{
privateGoodServicegoodService=newGoodServiceImpl();
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//解析集合
/*try{
List<Good>goods=goodService.findAll();
ListViewlistView=(ListView)this.findViewById(R.id.goods);
List<HashMap<String,Object>>data=newArrayList<HashMap<String,Object>>();
for(Goodgood:goods){
HashMap<String,Object>item=newHashMap<String,Object>();
item.put("name",good.getName());
item.put("price",getResources().getString(R.string.price)+good.getPrice());
item.put("id",good.getId());
data.add(item);
}
SimpleAdapteradapter=newSimpleAdapter(this,data,
R.layout.good,newString[]{"name","price"},
newint[]{R.id.name,R.id.price});
listView.setAdapter(adapter);
}catch(Exceptione){
Log.v("error","网络连接失败");
e.printStackTrace();
}*/
//解析单个对象
try{
Goodgood=goodService.findById();
ListViewlistView=(ListView)this.findViewById(R.id.goods);
List<HashMap<String,Object>>data=newArrayList<HashMap<String,Object>>();
HashMap<String,Object>item=newHashMap<String,Object>();
item.put("name",good.getName());
item.put("price",getResources().getString(R.string.price)+good.getPrice());
item.put("id",good.getId());
data.add(item);
SimpleAdapteradapter=newSimpleAdapter(this,data,
R.layout.good,newString[]{"name","price"},
newint[]{R.id.name,R.id.price});
listView.setAdapter(adapter);
}catch(Exceptione){
Log.v("error","网络连接失败");
e.printStackTrace();
}
}
}
通过以上方式就能实现struts2+android+json的开发了,希望你所有收获
以上内容归redarmychen版权所有,如想转载请附带出处,如有疑问请发邮件至redarmy.chen@gmail.com
原创:struts2+json+android开发整合解析终结
本文介绍如何在Android客户端使用Struts2+JSON进行网络访问操作,包括权限设置、业务Bean与Service类创建、商品信息解析及展示。

被折叠的 条评论
为什么被折叠?



