/** 如果网络畅通,则添加书签到网络* */ private void onAddMarksToServer(DiigoBookMarks marks) { client = new DiigoClient(this); marks = new DiigoBookMarks(); marks.setUrl(getViewString(addUrlEt)); marks.setTitle(getViewString(addTitleEt)); marks.setTags(getViewString(tagsAutoTv)); marks.setDesc(getViewString(addDescEt)); marks.setShared(getViewString(addSharedBox)); marks.setReadLater(getViewString(addReadLaterBox)); client.saveDiigoBookMarks( marks, new JsonHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, JSONObject response) { Log.d(TAG, "onSuccess response Object: " + response); // 添加成功,返回主页 /*Intent intent = new Intent(getBaseContext(), MainActivity.class); startActivity(intent);*/ } @Override public void onSuccess(int statusCode, Header[] headers, JSONArray response) { String respArray = response.toString(); Log.d(TAG, "onSuccess Array: " + respArray); } @Override public void onFailure( int statusCode, Header[] headers, String responseString, Throwable throwable) { String respString = responseString.toString(); Log.d(TAG, "onFailure response String: " + respString); } }); }
public void fetchNewDiigoBookMarks(final DiigoBookMarks marks, int Count) { // final Map<String, String> map = new HashMap<String, String>(); // allTags = new ArrayList(); // DiigoBookMarks marks = new DiigoBookMarks(); final ProgressDialog pDialog = new ProgressDialog(mCtx); pDialog.setMessage("Loading..."); pDialog.show(); client = new DiigoClient(mCtx); client.getDiigoBookMarks( marks, new JsonHttpResponseHandler() { @Override public void onFailure( int statusCode, Header[] headers, String responseString, Throwable throwable) { Toast.makeText(mCtx.getApplicationContext(), "Network Error", Toast.LENGTH_SHORT) .show(); Log.d(TAG, "statusCode: " + statusCode + "responseString: " + responseString); // pDialog.hide(); } @Override public void onFailure( int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) { Log.d(TAG, "statusCode: " + statusCode + "JSONArray errorResponse: " + errorResponse); // pDialog.hide(); } @Override public void onFailure( int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) { Log.d( TAG, "statusCode: " + statusCode + "; JSONObject errorResponse: " + errorResponse); // pDialog.hide(); } @Override public void onSuccess(int statusCode, Header[] headers, JSONArray response) { if (statusCode == 200) { String resp = response.toString(); Log.i(TAG, "response: " + resp); JSONArray newCacheMarks = new JSONArray(); // 从缓存文件中读取第一条信息的时间(最晚),和系统的比对,如果有比缓存的还晚,则加入缓存 String createAtFile = null; String cacheAllData = DiigoUtil.readMarksFromFile(); try { JSONArray cacheAllMarks = new JSONArray(cacheAllData); JSONObject firstMarks = (JSONObject) cacheAllMarks.get(0); createAtFile = (String) firstMarks.get("created_at"); int compare = 0; // 由于每次只能读取100 个书签,所以要遍历,读取所以书签 if (response.length() <= marks.getCount() && response.length() > 0) { for (int i = 0; i < response.length(); i++) { JSONObject jsonObject = response.getJSONObject(i); String createAtServer = (String) jsonObject.get("created_at"); Log.d( TAG, "createAtFile: " + createAtFile + ";createAtServer: " + createAtServer); compare = createAtFile.compareTo(createAtServer); Log.d(TAG, "compareTo: " + compare); // if (compare < 0) { newCacheMarks.put(jsonObject); Log.d(TAG, "newCacheMarks put Server Marks: " + jsonObject.toString()); } } // for end if (compare < 0) { marks.setStart(marks.getStart() + marks.getCount()); ; marks.setCount(marks.getCount()); fetchDiigoBookMarks(marks, marks.getCount()); Log.d(TAG, response.length() + ""); Log.d(TAG, "Marks Start" + marks.getStart()); } else { for (int i = 0; i < cacheAllMarks.length(); i++) { JSONObject itemMarks = (JSONObject) cacheAllMarks.get(i); newCacheMarks.put(itemMarks); } Log.d(TAG, "newCacheMarks leng: " + newCacheMarks.length() + ""); Log.d(TAG, "newCacheMarks: " + newCacheMarks.toString() + ""); Log.d(TAG, "readAllMarksJsonArr leng: " + readAllMarksJsonArr.length() + ""); Log.d(TAG, "readAllMarksJsonArr: " + readAllMarksJsonArr.toString() + ""); DiigoUtil.writeMarksToFile(newCacheMarks.toString()); } } else if (response.length() == 0) { Toast.makeText( mCtx.getApplicationContext(), "ReadLater complete", Toast.LENGTH_SHORT) .show(); Log.d(TAG, "ReadAll complete"); // 数据读取完,将读取的数据写入文件 // DiigoUtil.writeMarksToFile(readAllMarksJsonArr.toString()); } } catch (JSONException e) { e.printStackTrace(); } pDialog.hide(); } // statusCode end } }); }
/** 更新所有书签,并获取所有Tag */ public void fetchDiigoBookMarks(final DiigoBookMarks marks, int Count) { // DiigoBookMarks marks = new DiigoBookMarks(); final ProgressDialog pDialog = new ProgressDialog(mCtx); pDialog.setMessage("Loading..."); pDialog.show(); client = new DiigoClient(mCtx); client.getDiigoBookMarks( marks, new JsonHttpResponseHandler() { @Override public void onFailure( int statusCode, Header[] headers, String responseString, Throwable throwable) { Toast.makeText(mCtx.getApplicationContext(), "Network Error", Toast.LENGTH_SHORT) .show(); Log.d(TAG, "statusCode: " + statusCode + "responseString: " + responseString); // pDialog.hide(); } @Override public void onFailure( int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) { Log.d(TAG, "statusCode: " + statusCode + "JSONArray errorResponse: " + errorResponse); // pDialog.hide(); } @Override public void onFailure( int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) { Log.d( TAG, "statusCode: " + statusCode + "; JSONObject errorResponse: " + errorResponse); // pDialog.hide(); } @Override public void onSuccess(int statusCode, Header[] headers, JSONArray response) { if (statusCode == 200) { String resp = response.toString(); Log.i(TAG, "response: " + resp); // 由于每次只能读取100 个书签,所以要遍历,读取所以书签 if (response.length() <= marks.getCount() && response.length() > 0) { for (int i = 0; i < response.length(); i++) { try { JSONObject jsonObject = response.getJSONObject(i); // 获取书签,并将书签存放在Pfers 中 String tags = jsonObject.getString("tags"); String[] tag = tags.split(","); for (String s : tag) { Map<String, String> map = new HashMap<String, String>(); map.put(DiigoUtil.DIIGO_BOOKMARKS_TAGS + "-" + s, s); diigoUtil.setTagToPreference(map, DiigoUtil.DIIGO_BOOKMARKS_TAGS); } readAllMarksJsonArr.put(jsonObject); } catch (JSONException e) { e.printStackTrace(); } } // for end marks.setStart(marks.getStart() + marks.getCount()); ; marks.setCount(marks.getCount()); fetchDiigoBookMarks(marks, marks.getCount()); Log.d(TAG, response.length() + ""); Log.d(TAG, "Marks Start" + marks.getStart()); } else if (response.length() == 0) { Toast.makeText( mCtx.getApplicationContext(), "ReadLater complete", Toast.LENGTH_SHORT) .show(); Log.d(TAG, "ReadAll complete"); // 数据读取完,将读取的数据写入文件 DiigoUtil.writeMarksToFile(readAllMarksJsonArr.toString()); Log.d(TAG, "readAllMarksJsonArr leng: " + readAllMarksJsonArr.length() + ""); Log.d(TAG, "readAllMarksJsonArr: " + readAllMarksJsonArr.toString() + ""); } pDialog.hide(); } // statusCode end } }); }