asynctasks.LoadTaskTicketDataAsync.java Source code

Java tutorial

Introduction

Here is the source code for asynctasks.LoadTaskTicketDataAsync.java

Source

package asynctasks;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import common.CachedData;
import common.JSONSerializer;
import common.SessionUser;
import fragments.Inventory;
import fragments.Task;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.support.v4.app.FragmentManager;
import android.widget.Toast;

public class LoadTaskTicketDataAsync extends AsyncTask<String, Void, JSONObject> {

    private Context context;
    FragmentManager fm;

    private ProgressDialog pDialog;
    private JSONSerializer jSerial = new JSONSerializer();

    private String loadDataUrl = "http://www.chumlychum.com/valet/valetapi/task.php";

    public LoadTaskTicketDataAsync(Context context, FragmentManager fm) {
        this.context = context;
        this.fm = fm;
    }

    @Override
    protected JSONObject doInBackground(String... params) {

        SessionUser session = new SessionUser(context);

        List<NameValuePair> passer = new ArrayList<NameValuePair>();
        passer.add(new BasicNameValuePair("tag", "gettask"));
        passer.add(new BasicNameValuePair("currentstaff", session.userDetails().get("email")));
        passer.add(new BasicNameValuePair("task", params[0]));
        passer.add(new BasicNameValuePair("t_id", params[1]));
        passer.add(new BasicNameValuePair("tk_id", params[2]));
        JSONObject json = jSerial.getJSONFromUrl(loadDataUrl, "POST", passer);
        return json;
    }

    @Override
    protected void onPreExecute() {
        pDialog = new ProgressDialog(context);
        pDialog.setMessage("Retrieving Data...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    protected void onPostExecute(JSONObject json) {
        pDialog.cancel();
        ArrayList<HashMap<String, String>> cachedTempTickets = new ArrayList<HashMap<String, String>>();
        CachedData.getCachedTask().clear();
        int success, errorCode;
        try {
            success = json.getInt("success");
            if (success == 1) {
                JSONArray jsonList = json.getJSONArray("tickets");

                for (int i = 0; i < jsonList.length(); i++) {
                    JSONObject o = jsonList.getJSONObject(i);

                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put("t_id", o.getString("t_id"));
                    map.put("t_name", o.getString("t_name"));
                    map.put("t_serial", o.getString("t_serial"));
                    map.put("t_datein", o.getString("t_datein"));
                    map.put("t_dateout", o.getString("t_dateout"));
                    map.put("t_license", o.getString("t_license"));
                    map.put("t_vtype", o.getString("t_vtype"));
                    map.put("t_vcolor", o.getString("t_vcolor"));
                    map.put("t_vmodel", o.getString("t_vmodel"));
                    map.put("t_parklocation", o.getString("t_parklocation"));
                    map.put("t_keylocation", o.getString("t_keylocation"));
                    map.put("t_staffemailentered", o.getString("t_staffemailentered"));
                    map.put("t_useridpickup", o.getString("t_useridpickup"));
                    map.put("t_active", o.getString("t_active"));
                    map.put("tk_id", o.getString("tk_id"));

                    cachedTempTickets.add(map);
                }

                CachedData.setCachedTask(cachedTempTickets);

                Task resultFrag = (Task) fm.findFragmentByTag("Task");

                if (resultFrag != null) {
                    resultFrag.refreshData(CachedData.getCachedTask());
                }

            } else {
                Toast.makeText(context, "fuck, no work", Toast.LENGTH_SHORT).show();
            }

        } catch (JSONException e) {

            Task resultFrag = (Task) fm.findFragmentByTag("Task");
            if (resultFrag != null) {
                resultFrag.refreshData(CachedData.getCachedTask());
            }

            e.printStackTrace();
        }
    }

}