jeudi 12 février 2015

How to store data from Webservice to SQLite in Android?

This is my SQLiteDB class. Well, I just want to receive the values from web service and store the values on sqlite database.



public class SQLiteDB {

public static final String KEY_MERCHANT_KIOSK_ID = "merchant_kiosk_id";
public static final String KEY_MERCHANT_LOCATION_ID = "merchant_location_id";
public static final String KEY_USER_ID= "user_id";
public static final String KEY_NUMBER_OF_CHECKIN= "no_of_checkin";
public static final String KEY_FREE_GIFT = "free_gift";
public static final String KEY_VALID_DAYS= "valid_days";
public static final String KEY_CHECKIN_TIME_LIMIT= "checkin_time_limit";
public static final String KEY_BUSINESS_LOGO= "business_logo";
public static final String KEY_COUPON_ID="coupon_id";
public static final String KEY_COUPON_KEYWORD="coupon_keyword";
public static final String KEY_RANDOM_COUPON_CODE="random_coupon_code";
public static final String KEY_MERCHANT_ID="merchant_id";
public static final String KEY_ORGANIZATION_NAME="organization_name";
public static final String KEY_STATUS="status";
public static final String KEY_URL="url";


private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "SQLiteDB";

private static final String DATABASE_TABLE = "kiosktable";
private static final int DATABASE_VERSION = 1;

private static final String DATABASE_CREATE =
"create table kiosktable (merchant_kiosk_id text primary key, " +
"merchant_location_id text not null, " +
"user_id text not null, " +
"no_of_checkin text not null, " +
"free_gift text not null, " +
"valid_days text not null, " +
"checkin_time_limit text not null, " +
"business_logo text not null, " +
"coupon_id text not null, " +
"coupon_keyword text not null, " +
"random_coupon_code text not null, " +
"merchant_id text not null, " +
"organization_name text not null, " +
"status text not null, " +
"url text not null );";

private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;

public SQLiteDB(Context ctx) {

this.context = ctx;
DBHelper = new DatabaseHelper(context);
}

private static class DatabaseHelper extends SQLiteOpenHelper {

DatabaseHelper(Context context) {

super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {

try {
db.execSQL(DATABASE_CREATE);
} catch (SQLException e) {
e.printStackTrace();
}
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS kiosktable");
onCreate(db);
}
}

//---open SQLite DB---
public SQLiteDB open() throws SQLException {

db = DBHelper.getWritableDatabase();
return this;
}

//---close SQLite DB---
public void close() {

DBHelper.close();
}

//---insert data into SQLite DB---
public long insert(Integer merchant_kiosk_id, Integer merchant_location_id,
Integer user_id, Integer no_of_checkin, String free_gift, Integer valid_days,
Integer checkin_time_limit, String business_logo, Integer coupon_id,
int i, Integer random_coupon_code, Integer merchant_id,
String organization_name, Integer status, String url) {

ContentValues initialValues = new ContentValues();
initialValues.put(KEY_MERCHANT_KIOSK_ID, merchant_kiosk_id);
initialValues.put(KEY_MERCHANT_LOCATION_ID, merchant_location_id);
initialValues.put(KEY_USER_ID, user_id);
initialValues.put(KEY_NUMBER_OF_CHECKIN, no_of_checkin);
initialValues.put(KEY_FREE_GIFT, free_gift);
initialValues.put(KEY_VALID_DAYS, valid_days);
initialValues.put(KEY_CHECKIN_TIME_LIMIT, checkin_time_limit);
initialValues.put(KEY_BUSINESS_LOGO, business_logo);
initialValues.put(KEY_COUPON_ID, coupon_id);
initialValues.put(KEY_COUPON_KEYWORD, i);
initialValues.put(KEY_RANDOM_COUPON_CODE, random_coupon_code);
initialValues.put(KEY_MERCHANT_ID, merchant_id);
initialValues.put(KEY_ORGANIZATION_NAME, organization_name);
initialValues.put(KEY_STATUS, status);
initialValues.put(KEY_URL, url);


return db.insert(DATABASE_TABLE, null, initialValues);
}

//---Delete All Data from table in SQLite DB---
public void deleteAll() {

db.delete(DATABASE_TABLE, null, null);
}
}


Below is my URLActivity.java class. Any help regarding this would be a great help for me. Thanks in Advance.



SQLiteDB sqlite_obj;
List<String> list1,list2,list3,list4;

sqlite_obj = new SQLiteDB(UrlActivity.this);

try {

String url1 = edit_messageurl.getText().toString();
HttpClient httpclient = new DefaultHttpClient();
String httppostURL = "http: ...";
HttpPost httppost = new HttpPost(httppostURL);
Log.v(TAG, "postURL: " + httppost);

JSONObject data1 = new JSONObject();
data1.put("merchant_kiosk_url", url1);

JSONArray jsonArray = new JSONArray();
jsonArray.put(data1);

List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("data", data1.toString()));
httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));


httppost.setHeader(HTTP.CONTENT_TYPE,"application/x-www-form-urlencoded;charset=UTF-8");
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
String responseStr = EntityUtils.toString(resEntity).trim();
Log.v(TAG, "Response: " + responseStr);
Log.i("TAG",""+response.getStatusLine().getStatusCode());
// Toast.makeText(UrlActivity.this, responseStr, Toast.LENGTH_LONG).show();
// you can add an if statement here and do other actions based on the response
{

try
{


JSONObject mainObject = new JSONObject(responseStr);
String url = mainObject.getString("url");

if(url.equals("valid")){
ProgressBar1.setVisibility(View.GONE);
Toast.makeText(UrlActivity.this, responseStr, Toast.LENGTH_LONG).show();
try
{
sqlite();
Toast.makeText(getBaseContext(), "Stored in SQLite DB", Toast.LENGTH_SHORT).show();
} catch (android.database.sqlite.SQLiteException e) {
Log.e(TAG, "SQLiteException:" + e.getMessage());
Toast.makeText(getBaseContext(), "Stored in SQLite DB", Toast.LENGTH_SHORT).show();

}
}
else if(url.equals("invalid")){
ProgressBar1.setVisibility(View.GONE);
Toast.makeText(UrlActivity.this,"Invalid URL", Toast.LENGTH_LONG).show();
}

} catch (JSONException e) {

Log.e("JSON Parser", "Error parsing data " + e.toString());
ProgressBar1.setVisibility(View.GONE);
Toast.makeText(UrlActivity.this,"Invalid URL", Toast.LENGTH_LONG).show();
}
}
}
edit_messageurl.setText(""); //reset the message text field

//Toast.makeText(UrlActivity.this, "Data: " +data1,Toast.LENGTH_LONG).show();

} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Throwable t) {
Toast.makeText(UrlActivity.this, "Request failed: " + t.toString(),
Toast.LENGTH_LONG).show();
}

}

private void sqlite() {
// TODO Auto-generated method stub
sqlite_obj.open();

sqlite_obj.deleteAll();

for(int i=0; i<list1.size(); i++) {

sqlite_obj.insert(i, i, i, i, list1.get(i).toString(), i, i, list2.get(i).toString(), i, i, i, i, list3.get(i).toString(), i, list4.get(i).toString());
}

sqlite_obj.close();
}







});

Aucun commentaire:

Enregistrer un commentaire