Question

I'm developing my first client server android application. The client i.e, the android application gives request to find out the no of products submitted by the client in the server database. The server is sending the following data as string to the android application.

//server sending the response to client in this form.

{productid:1, productname:Google Nexus 5, productdescription:New Mobile Phone, productimage:Nexus 5.jpg, biddate:14-Feb-14 7:30:00 PM, currentdate:14-Feb-14 11:57:41 AM, productrate:33500}{productid:5, productname:Samsung Galaxy Ace, productdescription:Used Mobile Phone, productimage:Ace.jpg, biddate:15-Feb-14 7:30:00 PM, currentdate:14-Feb-14 11:57:41 AM, productrate:8500}

The above code is the output when the server sends the details of 2 products. I need to separate all the details as below and display as a list view. If there are n products as the response from the server, then there would be n items in the list view. I know the working of list view and its coding. But I don't know how to process this server response.

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

I have heard that this can be accomplished by using json deserializing. But I don't know how to do it. Can any one please help me out..

Was it helpful?

Solution

 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)

  }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top