com.anton.gavel.ComplaintSubmission.java Source code

Java tutorial

Introduction

Here is the source code for com.anton.gavel.ComplaintSubmission.java

Source

/*
 * This file is part of Gavel for Android.
    
Gavel for Android is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
Gavel for Android is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with Gavel for Android.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

package com.anton.gavel;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class ComplaintSubmission {
    Map<String, String> post_params = new HashMap<String, String>();
    Context mContext;

    public ComplaintSubmission(GavelMain context, PersonalInfo person, Map<String, String> complaint) {
        this.mContext = context;

        //form fields
        post_params.put("__VIEWSTATE",
                "dDwxNzUyNDY0MDEzO3Q8O2w8aTwzPjs+O2w8dDw7bDxpPDU+O2k8MTE+O2k8MTM+Oz47bDx0PDtsPGk8MD47aTwyPjs+O2w8dDxwPGw8VGV4dDs+O2w8XDxkaXYgY2xhc3M9InN1Ym5hdiJcPg0KICBcPHVsXD4NCiAgICBcPGxpXD4NCiAgICAgIFw8YSBocmVmPSIvQ2l0eURlcGFydG1lbnRzL0NvbW11bml0eVNlcnZpY2UvIlw+DQogICAgICAg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Bib3JkZXI9IjAiIGhlaWdodD0iMjAiIHdpZHRoPSI2MCJcPg0KICAgIFw8L2FcPg0KICBcPC9kaXZcPg0KICBcPGRpdiBjbGFzcz0idG9wIlw+DQogICAgXDxhIGhyZWY9IiN0b3BvZnBhZ2UiXD5Ub3Agb2YgcGFnZVw8L2FcPg0KICBcPC9kaXZcPg0KICBcPHBcPkNvcHlyaWdodCDCqSAyMDEyIGhhbWlsdG9uLmNhIC0gSGFtaWx0b24sIE9udGFyaW8sIENhbmFkYVw8L3BcPg0KXDwvZGl2XD47Pj47Oz47dDw7bDxpPDA+O2k8MT47aTwyPjs+O2w8dDxwPGw8V"
                        + "mlzaWJsZTs+O2w8bzxmPjs+Pjs7Pjt0PHA8bDxWaXNpYmxlOz47bDxvPGY+Oz4+Ozs+O3Q8cDxsPFZpc2libGU7PjtsPG88Zj47Pj47Oz47Pj47Pj47Pj47Pj47bDxfeHBjTWV0YURhdGE7Pj5OwHs/MekAhEgMO/LfVIAa/1s7iw==");
        post_params.put("COHShell:_ctl0:Button1", "Submit Form");
        post_params.put("idSearchString", "");

        //user information
        post_params.put("COHShell:_ctl0:qQ_FNAME", person.getField(R.id.first_name));
        post_params.put("COHShell:_ctl0:qQ_LNAME", person.getField(R.id.last_name));
        post_params.put("COHShell:_ctl0:qQ_HOMEPHONE", person.getField(R.id.primary_phone));
        post_params.put("COHShell:_ctl0:qQ_WORKNUMBER", person.getField(R.id.secondary_phone));
        post_params.put("COHShell:_ctl0:qQ_EMAIL", person.getField(R.id.email_address));
        post_params.put("COHShell:_ctl0:qQ_ADDRESS", person.getField(R.id.street_address));
        post_params.put("COHShell:_ctl0:qQ_PCODE", person.getField(R.id.postal_code));
        post_params.put("COHShell:_ctl0:qQ_CITY_CONTACT", person.getField(R.id.personal_cities_spinner));

        //complaint information
        post_params.put("COHShell:_ctl0:qQ_VADDRESS", complaint.get("location"));
        post_params.put("COHShell:_ctl0:qQ_CITY", complaint.get("city"));
        post_params.put("COHShell:_ctl0:qQ_VFNAME", complaint.get("firstName"));
        post_params.put("COHShell:_ctl0:qQ_VLNAME", complaint.get("lastName"));
        post_params.put("COHShell:_ctl0:qQ_COMPOLD", "");
        post_params.put("COHShell:_ctl0:qQ_COMP", complaint.get("complaint"));//extra mapping from spinner value to web form radio button value
        post_params.put("COHShell:_ctl0:qQ_OCOMP", complaint.get("otherComplaint"));
        post_params.put("COHShell:_ctl0:qQ_COMMENTS", complaint.get("complaintDetails"));

        //LogComplaint(post_params);

    }
    /*
    private void LogComplaint(Map<String,String> params){
       for (String key : params.keySet())
     Log.d("Runs", "\tfield: " +key + "\n\t\tvalue: "+params.get(key));
    }*/

    public void submit() {

        //Body of your click handler
        Thread trd = new Thread(new Runnable() {
            @Override
            public void run() {
                String formPath = "http://www.hamilton.ca/Hamilton.Portal/Templates/COHShell.aspx?NRMODE=Published&NRORIGINALURL=%2fCityDepartments%2fCorporateServices%2fITS%2fForms%2bin%2bDevelopment%2fMunicipal%2bLaw%2bEnforcement%2bOnline%2bComplaint%2bForm%2ehtm&NRNODEGUID=%7b4319AA7C-7E5E-4D65-9F46-CCBEC9AB86E0%7d&NRCACHEHINT=Guest";
                String hideString = "document.getElementById('mainform').style.display = 'none';";
                //this javascript will be added to the page if we are successful - we can look
                //for it to identify whether our form was successfully submitted

                // Create a new HttpClient and Post Header
                HttpClient httpClient = new DefaultHttpClient();

                ArrayList<NameValuePair> parameters = new ArrayList<NameValuePair>();

                for (String key : post_params.keySet()) {
                    parameters.add(new BasicNameValuePair(key, post_params.get(key)));
                    String value = post_params.get(key);
                    if (value != null && value.length() > 0) {
                        value = value.substring(0, Math.min(70, value.length()));
                    }
                    Log.d("Runs", "key: '" + key + "', value: '" + value);
                }
                //convert to a list of name-value pairs (dum, dee dum  -  duuuuumm...)

                HttpPost request = new HttpPost(formPath);

                //set up handler/bundle to give output to main thread
                Handler handler = ((GavelMain) mContext).submissionHandler;
                Message msg = handler.obtainMessage();
                Bundle bundle = new Bundle();

                try {
                    request.setEntity(new UrlEncodedFormEntity(parameters));

                    HttpResponse httpResponse = httpClient.execute(request);
                    HttpEntity responseEntity = httpResponse.getEntity();

                    String response = responseEntity == null ? ""
                            : EntityUtils.toString(responseEntity, EntityUtils.getContentCharSet(responseEntity));
                    //read the html page posted in response to our request

                    appendLog(response); // logs html page response to a text file on sd card

                    bundle.putBoolean("succeeded",
                            httpResponse.getStatusLine().getStatusCode() == 200 && response.contains(hideString));

                    msg.setData(bundle);
                    handler.sendMessage(msg);
                } catch (ClientProtocolException e) {
                    bundle.putBoolean("succeeded", false);
                    msg.setData(bundle);
                    handler.sendMessage(msg);
                } catch (IOException e) {
                    bundle.putBoolean("succeeded", false);
                    msg.setData(bundle);
                    handler.sendMessage(msg);
                } //code to do the HTTP request            
            }
        });
        trd.start();

        //

        return;

    }

    public void appendLog(String text) {
        File logFile = new File("sdcard/log.txt");
        if (logFile.exists())
            logFile.delete();
        if (!logFile.exists()) {
            try {
                logFile.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        try {
            //BufferedWriter for performance, true to set append to file flag
            BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
            buf.append(text);
            buf.newLine();
            buf.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}