我正在开发我的第一个客户端服务器android应用程序。客户端,即android应用程序提供请求,以找出客户端在服务器数据库中提交的产品的数量。服务器将以下数据作为字符串发送到android应用程序。
//服务器以这种形式向客户端发送响应。
{productid:1,产品名称:谷歌Nexus 5,产品描述:新手机,产品图片:Nexus 5.jpg,biddate:14-Feb-14 7:30:00 PM,currentdate:14-Feb- 14 11:57:41 AM,productrate:33500} {productid:5,产品名称:Samsung Galaxy Ace,产品描述:二手手机,产品图片:Ace.jpg,biddate:15-Feb-14 7:30:00 PM,currentdate :14-Feb-14 11:57:41 AM,productrate:8500}
以上代码是服务器发送2个产品的详细信息时的输出。我需要将所有细节分开如下,并显示为列表视图。如果有n个产品作为服务器的响应,那么列表视图中将有n个项目。我知道列表视图的工作及其编码。但我不知道如何处理此服务器响应。
productid[0]=1
productname[0]=Google Nexus 5
productdescription[0]=New Mobile Phone //this should be first item in the list
productimagename[0]=Nexus 5.jpg
biddate[0]=14-Feb-14 7:30:00 PM
currentdate[0]=14-Feb-14 11:57:41 AM
productrate[0]=33500
//similarly
productid[1]=5 //this is the second item to be displayed in the list view
productname[1]=Samsung Galaxy Ace
productdescription[1]=Used Mobile Phone
productimagename[1]=Ace.jpg
biddate[1]=15-Feb-14 7:30:00 PM
currentdate[1]=14-Feb-14 11:57:41 AM
productrate[1]=8500
我听说过这可以通过使用json反序列化来完成。但我不知道该怎么做。任何人都可以帮帮我..
答案 0 :(得分:1)
Class ProductInfo{
String productdescription;
String productid;
String productname;
String productimagename;
String biddate;
String currentdate;
String productrate;
}
Class ProductListInfo
{
ArrayList<ProductInfo> productList = new ArrayList<ProductInfo>();
}
public void parseResponse(Object response)
{
GSonBuilder gsonBuilder = new GSonBuilder();
GSon gson = gsonBuilder.create();
ProductListInfo listOfProducts = new ProductListInfo();
listOfProducts = gson.fromJson((String)response, ProductListInfo.class)
}
答案 1 :(得分:0)