// call by Asyntask when finish private void onGetJSON_Finish() { InstagramPhotoDbAdapter mDB = new InstagramPhotoDbAdapter(MainActivity.this); mDB.open(); int ii = 0; for (InstagramPhoto item : mPhotos) { if (ii % 2 == 0) item.is_favourite = 1; // random favourite ii++; // nếu title||img == rỗng thì b�? qua if (item.img_thumb_url.equals("") || item.title.equals("")) continue; if (mDB.getPhotoByInstagramId(item.id) != null) { // đã có trong CSDL mDB.updatePhoto(item.id, item); Log.w("qd", "Updated Instagram photo id " + item.id + " in SQLite db"); } else { // chưa có mDB.createPhoto(item); Log.w("qd", "Inserted new Instagram photo id " + item.id + " to SQLite db"); } } mDB.close(); // // then call show list view showList(); }
@Override protected Long doInBackground(String... params) { HttpURLConnection connection = null; try { Log.w("qd", "Open Internet connection for downloading JSON..."); URL dataUrl = new URL(API_PATH + "?access_token=" + API_TOKEN); // dataUrl = new URL("http://quocdung.net/test/json.html"); connection = (HttpURLConnection) dataUrl.openConnection(); connection.connect(); int status = connection.getResponseCode(); Log.d("connection", "status " + status); if (status == 200) { InputStream is = connection.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String responseString; StringBuilder sb = new StringBuilder(); while ((responseString = reader.readLine()) != null) { sb = sb.append(responseString); } String photoData = sb.toString(); mPhotos = InstagramPhoto.Load_From_RAW(photoData); Log.w("qd", "Get JSON raw data OK, API_PATH=" + dataUrl.getPath()); return (0l); } else { return (1l); } } catch (MalformedURLException e) { e.printStackTrace(); return (1l); } catch (IOException e) { System.out.println("IOException"); e.printStackTrace(); return (1l); } catch (NullPointerException e) { e.printStackTrace(); return (1l); } finally { connection.disconnect(); } }