asynctasks.AddTicketDataAsync.java Source code

Java tutorial

Introduction

Here is the source code for asynctasks.AddTicketDataAsync.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.JSONException;
import org.json.JSONObject;

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

import common.JSONSerializer;
import common.SessionUser;

import fragments.NewTicket;

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

    private ProgressDialog pDialog;
    private JSONSerializer jSerial = new JSONSerializer();
    private String addTicketUrl = "http://www.chumlychum.com/valet/valetapi/addticket.php";

    private Context context;
    private FragmentManager fm;

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

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

        SessionUser session = new SessionUser(context);
        HashMap<String, String> user = session.userDetails();
        String staffentered = user.get("email");

        List<NameValuePair> passer = new ArrayList<NameValuePair>();
        passer.add(new BasicNameValuePair("tag", "addticket"));
        passer.add(new BasicNameValuePair("ticket", params[0]));
        passer.add(new BasicNameValuePair("name", params[1]));
        passer.add(new BasicNameValuePair("date", params[2]));
        passer.add(new BasicNameValuePair("cartype", params[3]));
        passer.add(new BasicNameValuePair("model", params[4]));
        passer.add(new BasicNameValuePair("color", params[5]));
        passer.add(new BasicNameValuePair("license", params[6]));
        passer.add(new BasicNameValuePair("park", params[7]));
        passer.add(new BasicNameValuePair("key", params[8]));
        passer.add(new BasicNameValuePair("staff", staffentered));

        JSONObject json = jSerial.getJSONFromUrl(addTicketUrl, "POST", passer);

        return json;
    }

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

    @Override
    protected void onPostExecute(JSONObject json) {
        pDialog.cancel();
        int success;
        try {
            success = json.getInt("success");
            if (success == 1) {

                NewTicket frag = (NewTicket) fm.findFragmentByTag("New Ticket");
                frag.dismiss();

            } else {
                Toast.makeText(context, "ERROR IN SUBMISSION", Toast.LENGTH_SHORT).show();
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}