sorry, i'm new in adnroid programming , i try combination JSONParse and sqlite, when i store (insert) data to sqlite my column image = null (not save in sqlite), how to get image from url and store(insert) to sqlite ?
this is my code main activity
public class MainActivity extends Activity {
Button Btngetdata;
// URL to get JSON Array
private static String url = "http://ift.tt/176xioY";
private String IMAGEURL = "http://ift.tt/1zDPsW1";
private byte[] logoImage;
// JSON Node Names
private static final String TAG_DATA = "data";
public static final String TAG_IDT = "idt";
public static final String TAG_IDL = "idl";
public static final String TAG_NAMA = "nama";
public static final String TAG_ALAMAT = "alamat";
public static final String TAG_IMAGE = "image";
JSONArray user = null;
private DatabaseHandler databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
databaseHelper = new DatabaseHandler(MainActivity.this);
Btngetdata = (Button) findViewById(R.id.getdata);
Btngetdata.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new JSONParse().execute();
}
});
}
private class JSONParse extends AsyncTask<String, String, JSONObject> {
final String TAG = "AsyncTaskParseJson.java";
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Getting Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... args) {
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl(url);
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
pDialog.dismiss();
try {
// Getting JSON Array
user = json.getJSONArray(TAG_DATA);
// loop through all users
for (int i = 0; i < user.length(); i++) {
JSONObject c = user.getJSONObject(i);
// Storing JSON item in a Variable
String idt = c.getString(TAG_IDT);
String idl = c.getString(TAG_IDL);
String nama = c.getString(TAG_NAMA);
String alamat = c.getString(TAG_ALAMAT);
String image = c.getString(TAG_IMAGE);
// code for get image url and make byte
logoImage = getLogoImage(IMAGEURL+image);
databaseHelper.saveCategoryRecord(idt, idl, nama, alamat, image, logoImage);
// Set JSON Data in TextView
// uid.setText(idt);
//name1.setText(name);
// email1.setText(email);
// show the values in our logcat
Log.e(TAG, "nama >>>> " + idt + " alamat>>>>> " + nama);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
// try get url and make byte
private byte[] getLogoImage(String geturl) {
try {
URL imageUrl = new URL(geturl);
URLConnection ucon = imageUrl.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(500);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
return baf.toByteArray();
} catch (Exception e) {
Log.d("ImageManager", "Error: " + e.toString());
}
return null;
}
}
and this is mydatabase handler
public class DatabaseHandler {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "bantul";
private static final String TABLE_NAME = "tb_alldata";
public static final String TAG_IDT = "idt";
public static final String TAG_IDL = "idl";
public static final String TAG_NAMA = "nama";
public static final String TAG_ALAMAT = "alamat";
public static final String TAG_GAMBAR = "gambar";
public static final String TAG_FILE_IMAGE = "file_image";
Category openHelper;
private SQLiteDatabase database;
public DatabaseHandler(Context context){
openHelper = new Category(context);
database = openHelper.getWritableDatabase();
}
public void saveCategoryRecord(String idt, String idl, String nama, String alamat, String gambar, byte[] logoImage) {
ContentValues contentValues = new ContentValues();
contentValues.put(TAG_IDT, idt);
contentValues.put(TAG_IDL, idl);
contentValues.put(TAG_NAMA, nama);
contentValues.put(TAG_ALAMAT, alamat);
contentValues.put(TAG_GAMBAR, gambar);
contentValues.put(TAG_FILE_IMAGE, logoImage);
database.insert(TABLE_NAME, null, contentValues);
}
public Cursor getTimeRecordList() {
return database.rawQuery("select * from " + TABLE_NAME, null);
}
private class Category extends SQLiteOpenHelper {
public Category(Context context) {
// TODO Auto-generated constructor stub
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + TABLE_NAME + "( "
+ TAG_IDT + " INTEGER PRIMARY KEY, "
+ TAG_IDL + " INTEGER, "+ TAG_NAMA + " TEXT, "+ TAG_ALAMAT + " TEXT, "+ TAG_GAMBAR + " TEXT, "
+ TAG_FILE_GAMAR + " BLOB, )" );
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS"+ TABLE_NAME);
onCreate(db);
}
}
Aucun commentaire:
Enregistrer un commentaire