asynctasks.LoadTicketDataAsync.java Source code

Java tutorial

Introduction

Here is the source code for asynctasks.LoadTicketDataAsync.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 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 LoadTicketDataAsync 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/getdata.php";

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

    @Override
    protected JSONObject doInBackground(String... params) {
        List<NameValuePair> passer = new ArrayList<NameValuePair>();
        passer.add(new BasicNameValuePair("tag", "getdata"));
        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.getCachedTickets().clear();
        int success;
        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"));

                    cachedTempTickets.add(map);
                }

                CachedData.setCachedTickets(cachedTempTickets);

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

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

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

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}