hi i wan save JsonObject into sqliteDB .and show in list view.
public class MainActivity extends ListActivity
InputStream is = null;
String ip = "http://ift.tt/1tk9msb";
String line = null;
String result = null;
public String[] list1, list2;
SQLiteDB sqlite_obj;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sqlite_obj = new SQLiteDB(MainActivity.this);
try {
URL url = new URL(ip);
executeReq(url);
webserviceCall();
Toast.makeText(getBaseContext(), "Data from Webservice", Toast.LENGTH_SHORT).show();
}
catch(Exception e) {
sqlitecall();
Toast.makeText(getBaseContext(), "Data from SQLite DB", Toast.LENGTH_SHORT).show();
}
setListAdapter(new ListViewAct(this, list1, list2));
}
private void executeReq(URL url) throws IOException {
// TODO Auto-generated method stub
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setReadTimeout(3000);
con.setConnectTimeout(3500);
con.setRequestMethod("GET");
con.setDoInput(true);
// Connect
con.connect();
}
private void webserviceCall() {
// TODO Auto-generated method stub
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(ip);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
catch (Exception e) {
Log.e("Webservice 1", e.toString());
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
}
catch (Exception e) {
Log.e("Webservice 2", e.toString());
}
try {
JSONArray ja = new JSONArray(result);
JSONObject jo = null;
list1 = new String[ja.length()];
list2 = new String[ja.length()];
for(int i=0; i<ja.length(); i++) {
jo = ja.getJSONObject(i);
list1[i] = jo.getString("id");
list2[i] = jo.getString("name");
}
}catch (Exception e) {
Log.e("Webservice 3", e.toString());
}
sqlite_obj.open();
sqlite_obj.deleteAll();
for(int i=0; i<list1.length; i++) {
sqlite_obj.insert(list1[i].toString(), list2[i].toString());
}
sqlite_obj.close();
}
private void sqlitecall() {
// TODO Auto-generated method stub
sqlite_obj.open();
Cursor c = sqlite_obj.getAllData();
list1 = new String[c.getCount()];
list2 = new String[c.getCount()];
if (c.moveToFirst())
{
int i=0;
do {
DisplayContact(c, i);
i++;
} while (c.moveToNext());
}
sqlite_obj.close();
}
private void DisplayContact(Cursor c, int i) {
// TODO Auto-generated method stub
list1[i] = c.getString(0);
list2[i] = c.getString(1);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
//get selected items
String selectedValue = (String) getListAdapter().getItem(position);
Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();
}
and sqliteDB.java
public class SQLiteDB {
public static final String KEY_ID = "id";
public static final String KEY_NAME = "name";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "SQLiteDB";
private static final String DATABASE_TABLE = "sample";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE =
"create table sample (id text primary key, name 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 sample");
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(String id, String name) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_ID, id);
initialValues.put(KEY_NAME, name);
return db.insert(DATABASE_TABLE, null, initialValues);
}
//---Delete All Data from table in SQLite DB---
public void deleteAll() {
db.delete(DATABASE_TABLE, null, null);
}
//---Get All Contacts from table in SQLite DB---
public Cursor getAllData() {
return db.query(DATABASE_TABLE, new String[] {KEY_ID, KEY_NAME},
null, null, null, null, null);
}
} and ListViweAct
public class ListViewAct extends ArrayAdapter {
private final Context context;
private final String[] values1;
private final String[] values2;
public ListViewAct(Context context, String[] object1, String[] object2) {
super(context, R.layout.activity_main, object2);
this.context = context;
this.values1 = object1;
this.values2 = object2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.activity_main, parent, false);
TextView textView1 = (TextView) rowView.findViewById(R.id.list1);
TextView textView2 = (TextView) rowView.findViewById(R.id.list2);
textView1.setText(values1[position]);
textView2.setText(values2[position]);
return rowView;
}
LogCat : max_texture_size :16384 Getting max_Texture_size from caches:initConstrants
Aucun commentaire:
Enregistrer un commentaire