lundi 8 juin 2015

How to store array response from the web service to android sqlite database

I want to insert array of XML result from a web service in to my android sqlite data base for generating a list view, how can i get this array of string from web service and insert in to sqlite DB by calling the web service, below is my code details.

XML result:

   <ArrayOfUser xmlns:xsi="http://ift.tt/ra1lAU" xmlns:xsd="http://ift.tt/tphNwY" xmlns="http://tempuri.org/">
<User>
<from_mobileno>4359835745</from_mobileno>
<to_mobileno>6757575</to_mobileno>
<to_contactname>xxxx</to_contactname>
<priority1>1</priority1>
<priority2>0</priority2>
<priority3>0</priority3>
<panic_type>Indoor</panic_type>
<status>A</status>
</User>
<User>
<from_mobileno>+4359835745</from_mobileno>
<to_mobileno>3634653</to_mobileno>
<to_contactname>yyyy</to_contactname>
<priority1>1</priority1>
<priority2>2</priority2>
<priority3>0</priority3>
<panic_type>Outdoor</panic_type>
<status>A</status>
</User>
</ArrayOfUser>

Sqlite DB:

public class RegisterConactDBAdapter {



    SQLiteOpenHelper dbhelper;
    SQLiteDatabase database;
    String LOGTAG="RegisterContactDBAdapter";

    static final String REGCONTACT_TABLE="regcontact";
    private static final String SNO="si_no";
    private static final String FROMMOBILENO="from_mobileno";
    private static final String CONTACTNAME="contact_name";
    private static final String TOMOBILENO="to_mobileno";
    private static final String PRIORITY1="priority1";
    private static final String PRIORITY2="priority2";
    private static final String PRIORITY3="priority3";
    private static final String PANICTYPE="panic_type";
    private static final String STATUS="status";


    private static final String[] allColumns={
        SNO,
        FROMMOBILENO,
        CONTACTNAME,
        TOMOBILENO,
        PRIORITY1,
        PRIORITY2,
        PRIORITY3,
        PANICTYPE,
        STATUS


    };


    public static  String createRegisterContactTable(){ 
         String CREATE_REGCONTACTTABLE="CREATE TABLE " + REGCONTACT_TABLE + "("
            + SNO+ " INTEGER PRIMARY KEY AUTOINCREMENT" + "," 
            + FROMMOBILENO+ " TEXT" + "," 
            + CONTACTNAME+ " TEXT" + "," 
            + TOMOBILENO+ " TEXT" + ","
            + PRIORITY1+ " TEXT" + ","
            + PRIORITY2+ " TEXT" + ","
            + PRIORITY3+ " TEXT" + ","
            + PANICTYPE+ " TEXT" + ","
            + STATUS+" TEXT"  +

            ")";
        return CREATE_REGCONTACTTABLE;
    }
        public RegisterConactDBAdapter(Context context){
            Log.i(LOGTAG,"RegisterConactDBAdapter...");
            dbhelper= new DBHandler(context);
        }

        public void open(){
            database = dbhelper.getWritableDatabase();
            Log.i(LOGTAG,"Database Opened");
        }

        public void close(){
            dbhelper.close();
            Log.i(LOGTAG,"Database Closed");

}

public void CreateUser(Contact contact){

                ContentValues values= new ContentValues();
        values.put(FROMMOBILENO,contact.getFromMobileno());
        values.put(CONTACTNAME,contact.getContactName());
        values.put(TOMOBILENO,contact.getToMobileno());
        values.put(PRIORITY1,contact.getPriority1());
        values.put(PRIORITY2,contact.getPriority2());
        values.put(PRIORITY3,contact.getPriority3());
        values.put(PANICTYPE, contact.getPanicType());
        values.put(STATUS,contact.getStatus());
        Log.i("Fisrt Row Id...",SNO);
        database.insert(REGCONTACT_TABLE, null, values);

    }


}

Aucun commentaire:

Enregistrer un commentaire