Android Open Source - amulet Send Task Entries To Server






From Project

Back to project page amulet.

License

The source code is released under:

MIT License

If you think the Android project amulet listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package scamell.michael.amulet;
/*from www. j  av a 2 s  .  c o  m*/

import android.content.Context;
import android.util.Log;

import org.apache.http.entity.StringEntity;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;


public class SendTaskEntriesToServer implements OnPostHttpData {

    private static final String taskServerURL = "http://08309.net.dcs.hull.ac.uk/api/admin/task";

    private final Context context;
    private final TaskEntries taskEntries;

    SendTaskEntriesToServer(Context context, TaskEntries taskEntries) {
        this.context = context;
        this.taskEntries = taskEntries;
    }

    /**
     * -post reference
     * http://hmkcode.com/android-send-json-data-to-server/
     * -setting header content type ref
     * http://stackoverflow.com/questions/14119410/json-not-working-with-httppost-probably-around-setentity
     */
    public void sendTaskEntriesToServer() {
        StringEntity se = null;
        JSONObject taskEntriesJSONObject = TaskEntries.buildTaskEntriesJSONObjectForServer(context, taskEntries);
        String taskEntriesJSON = taskEntriesJSONObject.toString();
        try {
            se = new StringEntity(taskEntriesJSON);
            se.setContentType("application/json");
        } catch (UnsupportedEncodingException e) {
            Log.e("ERROR_UNSUPPORTED_ENCODING_EXCEPTION", "Couldn't create string entity for post");
            e.printStackTrace();
        }
        PostHTTPDataAsync postDrinkDiaryEntries = new PostHTTPDataAsync(this, se);
        postDrinkDiaryEntries.execute(taskServerURL);
    }

    @Override
    public void onPostTaskCompleted(String httpData) {
        Log.i("INFO_SERVER_RESPONSE_DRINK_DIARY", httpData);
        checkServerResponseToPost(httpData);
    }

    public void checkServerResponseToPost(String response) {
        if (response.contains("success") || response.contains("received")) {
            Log.i("TASK_ENTRIES_REUPLOAD", "Task entries uploaded successfully," + response);
            //each time it's successful set it to false. See MainActivity onCreate for why
            SharedPreferencesWrapper.saveToPrefs(context, "reUpload_tasks", false);
        } else if (response.equals("INVALID")) {
            Log.w("WARNING_SERVER_RESPONSE", "Drink diary entry sent was invalid");
        } else {
            Log.e("TASK_ENTRIES_REUPLOAD", "Re-upload of task entries failed");
            SharedPreferencesWrapper.saveToPrefs(context, "reUpload_tasks", true);
        }
    }
}




Java Source Code List

scamell.michael.amulet.AboutDialogFragment.java
scamell.michael.amulet.AccountDetailsFragment.java
scamell.michael.amulet.ChangePasswordDialogFragment.java
scamell.michael.amulet.CoolveticaTextView.java
scamell.michael.amulet.DateAndTime.java
scamell.michael.amulet.DrinkDiaryEntries.java
scamell.michael.amulet.DrinkDiaryEntryAdapter.java
scamell.michael.amulet.DrinkDiaryEntry.java
scamell.michael.amulet.DrinkDiaryFavouriteDrinksDialogFragment.java
scamell.michael.amulet.DrinkDiaryFragment.java
scamell.michael.amulet.ExampleDrinksDialogFragment.java
scamell.michael.amulet.FavouriteDrinkUtility.java
scamell.michael.amulet.HomeFragment.java
scamell.michael.amulet.InspectionTaskActivity.java
scamell.michael.amulet.InspectionTaskInstructions1Fragment.java
scamell.michael.amulet.InspectionTaskInstructions2Fragment.java
scamell.michael.amulet.LastTaskInstructionFragment.java
scamell.michael.amulet.LoginActivity.java
scamell.michael.amulet.LogoutDialogFragment.java
scamell.michael.amulet.MainActivity.java
scamell.michael.amulet.OnPostHttpData.java
scamell.michael.amulet.OnRetrieveHttpData.java
scamell.michael.amulet.PostHTTPDataAsync.java
scamell.michael.amulet.RegisterActivity.java
scamell.michael.amulet.ResumeTaskDialogFragment.java
scamell.michael.amulet.RetrieveHTTPDataAsync.java
scamell.michael.amulet.RetrieveHTTPData.java
scamell.michael.amulet.RetrieveTaskDataFromServer.java
scamell.michael.amulet.SaveDrinkDiaryUtility.java
scamell.michael.amulet.SendDrinkDiaryEntriesToServer.java
scamell.michael.amulet.SendDrinkDiaryEntryToServer.java
scamell.michael.amulet.SendTaskEntriesToServer.java
scamell.michael.amulet.SendTaskEntryToServer.java
scamell.michael.amulet.SequenceTaskActivity.java
scamell.michael.amulet.SequenceTaskInstruction1Fragment.java
scamell.michael.amulet.SequenceTaskInstruction2Fragment.java
scamell.michael.amulet.SequenceTaskInstruction3Fragment.java
scamell.michael.amulet.SharedPreferencesWrapper.java
scamell.michael.amulet.Sound.java
scamell.michael.amulet.TaskEntries.java
scamell.michael.amulet.TaskEntry.java
scamell.michael.amulet.TaskFinishFragment.java
scamell.michael.amulet.TaskInstructionsActivity.java
scamell.michael.amulet.TaskStartCountdownFragment.java
scamell.michael.amulet.TasksFirstStartWelcomeFragment.java
scamell.michael.amulet.TasksMenuFragment.java
scamell.michael.amulet.UnitCalculatorFavouriteDrinksDialogFragment.java
scamell.michael.amulet.UnitCalculatorFragment.java
scamell.michael.amulet.UnitCalculator.java