Search code examples
androidweb-servicessqliteandroid-contentproviderksoap2

how to get data from webservice and store that on sqlite in android?


I have got the response from webservice using KSOAP2. The response xml is as bellow:

<env:Envelope
  xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:ns0="http://wsclient.xyz.com/">
 <env:Body>
  <ns0:getResponseData>

   <ns0:result>
    <ns0:wef> 12-June-2002 </ns0:wef>      
    <ns0:sblanguage> SOME TEXT.....</ns0:sblanguage>
    <ns0:payscale> 15700-400-19900 </ns0:payscale>
    <ns0:entrytakenby  xsi:nil="1"/>
    <ns0:entryHeader> PAY FIXATION</ns0:entryHeader>
    <ns0:postcadre   xsi:nil="1"/>    
   </ns0:result>

   <ns0:result>
    <ns0:wef> 20-JuLY-2010 </ns0:wef>      
    <ns0:sblanguage> SOME MORE TEXT.....</ns0:sblanguage>
    <ns0:payscale> 19700-600-22900 </ns0:payscale>
    <ns0:entrytakenby> CLERK </ns0:entrytakenby>
    <ns0:entryHeader> INCREMENT </ns0:entryHeader>
    <ns0:postcadre   xsi:nil="1"/>    
   </ns0:result>

   // .... hundreds of such results

   </ns0:getResponseData>
 </env:Body>
</env:Envelope>

My objective is to display all these info in tabular form in on a fragment. I have created the UI for the fragment and wrote an CustomCursorAdapter for the fragment. I have successfully displayed dummy data (not from webservice) on the fragment. Now I want to know

1) How can I create a cursor from the above data to use that in the CustomCursorAdapter for the fragment.

2) I know, I have to use sqlite to store these data so that it can be used with out internet connection, once fetched from the web. I am confused about the steps in achieving these two objectives.

pls, show me some pointers on this regard. I had gone through some links but could get enough information to link all these pieces together.

Edit: I do not need the code but the strategy to implement will be helpful. Do I need to parse the xml and store the data in an object? Then populate the db using the object? How such case is implemented?


Solution

  • I think you should create an AsyncTask that downloads all your data, and after that, in your onPostExecute, you can save your data in database. Usually when you do the deserialization, from XML or Json, you create the objects associated with your data. So, you can create helper method that insert these data in your database. Then you can use your Cursor for showing data in your ListView (or other) inside your Fragment.

    You can also try a different strategy: you can create a listener for your database, so that when data are added in your tables, automatically your UI is updated. For this purpose you can consider using ContentObserver. So, when your AsyncTask download your data and put them in the database, automatically ListView is populated.