Code Clean-Up (purged some String literals)

This commit is contained in:
Stefan Niedermann 2015-10-06 23:56:02 +02:00
parent 2f7c66f744
commit 04f335d2c6

View file

@ -23,6 +23,18 @@ import it.niedermann.owncloud.notes.model.Note;
public class NotesClient { public class NotesClient {
private static final String key_id = "id";
private static final String key_title = "title";
private static final String key_content = "content";
private static final String key_modified = "id";
private static final String application_json = "application/json";
private static final String method_get = "GET";
private static final String method_put = "PUT";
private static final String method_post = "POST";
private static final String method_delete = "DELETE";
private String url = ""; private String url = "";
private String username = ""; private String username = "";
private String password = ""; private String password = "";
@ -36,7 +48,7 @@ public class NotesClient {
public List<Note> getNotes() throws JSONException, public List<Note> getNotes() throws JSONException,
IOException { IOException {
List<Note> notesList = new ArrayList<>(); List<Note> notesList = new ArrayList<>();
JSONArray notes = new JSONArray(requestServer("notes", "GET", null)); JSONArray notes = new JSONArray(requestServer("notes", method_get, null));
long noteId = 0; long noteId = 0;
String noteTitle = ""; String noteTitle = "";
String noteContent = ""; String noteContent = "";
@ -45,19 +57,19 @@ public class NotesClient {
for (int i = 0; i < notes.length(); i++) { for (int i = 0; i < notes.length(); i++) {
currentItem = notes.getJSONObject(i); currentItem = notes.getJSONObject(i);
if (!currentItem.isNull("id")) { if (!currentItem.isNull(key_id)) {
noteId = currentItem.getLong("id"); noteId = currentItem.getLong(key_id);
} }
if (!currentItem.isNull("title")) { if (!currentItem.isNull(key_title)) {
noteTitle = currentItem.getString("title"); noteTitle = currentItem.getString(key_title);
} }
if (!currentItem.isNull("content")) { if (!currentItem.isNull(key_content)) {
noteContent = currentItem.getString("content"); noteContent = currentItem.getString(key_content);
} }
if (!currentItem.isNull("modified")) { if (!currentItem.isNull(key_modified)) {
noteModified = GregorianCalendar.getInstance(); noteModified = GregorianCalendar.getInstance();
noteModified noteModified
.setTimeInMillis(currentItem.getLong("modified") * 1000); .setTimeInMillis(currentItem.getLong(key_modified) * 1000);
} }
notesList notesList
.add(new Note(noteId, noteModified, noteTitle, noteContent)); .add(new Note(noteId, noteModified, noteTitle, noteContent));
@ -66,7 +78,7 @@ public class NotesClient {
} }
/** /**
* Fetches on Note by ID from Server * Fetches a Note by ID from Server
* TODO Maybe fetch only id, title and modified from server until a note has been opened? * TODO Maybe fetch only id, title and modified from server until a note has been opened?
* @param id long - ID of the wanted note * @param id long - ID of the wanted note
* @return Requested Note * @return Requested Note
@ -81,21 +93,21 @@ public class NotesClient {
String noteContent = ""; String noteContent = "";
Calendar noteModified = null; Calendar noteModified = null;
JSONObject currentItem = new JSONObject( JSONObject currentItem = new JSONObject(
requestServer("notes/" + id, "GET", null)); requestServer("notes/" + id, method_get, null));
if (!currentItem.isNull("id")) { if (!currentItem.isNull(key_id)) {
noteId = currentItem.getLong("id"); noteId = currentItem.getLong(key_id);
} }
if (!currentItem.isNull("title")) { if (!currentItem.isNull(key_title)) {
noteTitle = currentItem.getString("title"); noteTitle = currentItem.getString(key_title);
} }
if (!currentItem.isNull("content")) { if (!currentItem.isNull(key_content)) {
noteContent = currentItem.getString("content"); noteContent = currentItem.getString(key_content);
} }
if (!currentItem.isNull("modified")) { if (!currentItem.isNull(key_modified)) {
noteModified = GregorianCalendar.getInstance(); noteModified = GregorianCalendar.getInstance();
noteModified noteModified
.setTimeInMillis(currentItem.getLong("modified") * 1000); .setTimeInMillis(currentItem.getLong(key_modified) * 1000);
} }
return new Note(noteId, noteModified, noteTitle, noteContent); return new Note(noteId, noteModified, noteTitle, noteContent);
} }
@ -115,23 +127,23 @@ public class NotesClient {
Calendar noteModified = null; Calendar noteModified = null;
JSONObject paramObject = new JSONObject(); JSONObject paramObject = new JSONObject();
paramObject.accumulate("content", content); paramObject.accumulate(key_content, content);
JSONObject currentItem = new JSONObject(requestServer("notes", "POST", JSONObject currentItem = new JSONObject(requestServer("notes", method_post,
paramObject)); paramObject));
if (!currentItem.isNull("id")) { if (!currentItem.isNull(key_id)) {
noteId = currentItem.getLong("id"); noteId = currentItem.getLong(key_id);
} }
if (!currentItem.isNull("title")) { if (!currentItem.isNull(key_title)) {
noteTitle = currentItem.getString("title"); noteTitle = currentItem.getString(key_title);
} }
if (!currentItem.isNull("content")) { if (!currentItem.isNull(key_content)) {
noteContent = currentItem.getString("content"); noteContent = currentItem.getString(key_content);
} }
if (!currentItem.isNull("modified")) { if (!currentItem.isNull(key_modified)) {
noteModified = GregorianCalendar.getInstance(); noteModified = GregorianCalendar.getInstance();
noteModified noteModified
.setTimeInMillis(currentItem.getLong("modified") * 1000); .setTimeInMillis(currentItem.getLong(key_modified) * 1000);
} }
return new Note(noteId, noteModified, noteTitle, noteContent); return new Note(noteId, noteModified, noteTitle, noteContent);
} }
@ -142,24 +154,24 @@ public class NotesClient {
Calendar noteModified = null; Calendar noteModified = null;
JSONObject paramObject = new JSONObject(); JSONObject paramObject = new JSONObject();
paramObject.accumulate("content", content); paramObject.accumulate(key_content, content);
JSONObject currentItem = new JSONObject(requestServer( JSONObject currentItem = new JSONObject(requestServer(
"notes/" + noteId, "PUT", paramObject)); "notes/" + noteId, method_put, paramObject));
if (!currentItem.isNull("title")) { if (!currentItem.isNull(key_title)) {
noteTitle = currentItem.getString("title"); noteTitle = currentItem.getString(key_title);
} }
if (!currentItem.isNull("modified")) { if (!currentItem.isNull(key_modified)) {
noteModified = GregorianCalendar.getInstance(); noteModified = GregorianCalendar.getInstance();
noteModified noteModified
.setTimeInMillis(currentItem.getLong("modified") * 1000); .setTimeInMillis(currentItem.getLong(key_modified) * 1000);
} }
return new Note(noteId, noteModified, noteTitle, content); return new Note(noteId, noteModified, noteTitle, content);
} }
public void deleteNote(long noteId) throws public void deleteNote(long noteId) throws
IOException { IOException {
this.requestServer("notes/" + noteId, "DELETE", null); this.requestServer("notes/" + noteId, method_delete, null);
} }
/** /**
@ -188,7 +200,7 @@ public class NotesClient {
con.setConnectTimeout(10 * 1000); // 10 seconds con.setConnectTimeout(10 * 1000); // 10 seconds
if (params != null) { if (params != null) {
con.setFixedLengthStreamingMode(params.toString().getBytes().length); con.setFixedLengthStreamingMode(params.toString().getBytes().length);
con.setRequestProperty("Content-Type", "application/json"); con.setRequestProperty("Content-Type", application_json);
con.setDoOutput(true); con.setDoOutput(true);
OutputStream os = con.getOutputStream(); OutputStream os = con.getOutputStream();
Log.v("Note", params.toString()); Log.v("Note", params.toString());