Android HttpClient Asynctask-Collection of common programming errors

I have been stuck on this problem for a day or two now, so i decided to see if anyone out there could help me.

The current goal i have, is to make a HttpPost that connects to, and executes a script that i have on my Wamp server. I have implemented a class that extends AsyncTask, and includes the 3 necessary methods, that are required in order for it to work effectively.

I will first show you the code that i have used to put the variables into a Json Object, followed by the JSONParser class, that initializes the HttpPost and then executes it.
Afterwards i will tell you all about the log errors and the narrowing down of the problem; you all problem know what the problem may be already, and if you don’t want to read below, briefly, it is caused by the line of code that calls the parser class.

Note: I am using this as a starting point for this type of work, so please understand that it is simple in terms of its passed parameters.

Further Note: I am using Eclipse, and i am testing with the inbuilt Emulator.

METHOD – CreateNewUser

/**
 * Background Async Task to Create new user
 */
class CreateNewUser extends AsyncTask{

/**
* Before starting background thread show progress dialog
*/
@Override
protected void onPreExecute(){
    super.onPreExecute();
    pDialog = new ProgressDialog(AddUserActivity.this);
    pDialog.setIndeterminate(false);
    pDialog.setCancelable(true);
    pDialog.show();
}

/*
* Creating user
*/
@Override
protected String doInBackground(String... args){

    String username = inputUsername.getText().toString();
    String password = inputPassword.getText().toString();

    // Building parameters
    List params = new ArrayList();
    params.add(new BasicNameValuePair("username", username));
    params.add(new BasicNameValuePair("password", password));

    // getting JSON object
    // Note that create user url accepts POST method
       JSONObject json = jsonParser.makeHttpRequest(url_create_user, "POST",params);               

    // check log cat for response
    Log.d("Create Response", json.toString());

        // check for success tag
        try {
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // successfully created product
                Intent i = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(i);

                // closing this screen
                finish();
            } else {
                // failed to create product
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;    
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    @Override
    protected void onPostExecute(String file_url) {
        // dismiss the dialog once done
        pDialog.dismiss();
    }
}

Next, the JSONParser Class:

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }

    // function get JSON from URL
    // by making HTTP POST or GET method
    public JSONObject makeHttpRequest(String url, String method,
            List params) {

        // Making HTTP request
        try {

            // check for request method
            if(method == "POST"){
                // request method is POST
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));

                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();

            }else if(method == "GET"){
                // request method is GET
                DefaultHttpClient httpClient = new DefaultHttpClient();
                String paramString = URLEncodedUtils.format(params, "utf-8");
                url += "?" + paramString;
                HttpGet httpGet = new HttpGet(url);

                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
            }           

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
       return jObj;

    }
}

If i remove the line : ( JSONObject json = jsonParser.makeHttpRequest(url_create_user, "POST",params); ) – of course with intending the try catch out, then the program does not crash, when i press the button that calls the CreateNewUser class.

If i do not do that, my program brings up a loading screen that swirls around until it becomes unresponsive, and asks me to close down the application.

The logs describe Async errors, and illegal state ones:

E/AndroidRuntime(1132): FATAL EXCEPTION: AsyncTask #2
E/AndroidRuntime(1132): Process: com.example.propertypanther, PID: 1132
E/AndroidRuntime(1132): java.lang.RuntimeException: An error occured while executing   
doInBackground()
E/AndroidRuntime(1132):     at android.os.AsyncTask$3.done(AsyncTask.java:300)
E/AndroidRuntime(1132):     at  
 java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
E/AndroidRuntime(1132):     at    
java.util.concurrent.FutureTask.setException(FutureTask.java:222)
E/AndroidRuntime(1132):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
E/AndroidRuntime(1132):     at   
android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
E/AndroidRuntime(1132):     at 
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
E/AndroidRuntime(1132):     at
 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
E/AndroidRuntime(1132):     at java.lang.Thread.run(Thread.java:841)
E/AndroidRuntime(1132): Caused by: java.lang.IllegalStateException: Target host must not
be null, or set in parameters. scheme=null, host=null,    
path=localhost/android_connect/sqlconfig/create_user.php
E/AndroidRuntime(1132):     at   
org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.jav
a:591)
E/AndroidRuntime(1132):     at  
org.apache.http.impl.client.DefaultRequestDirector.execute
(DefaultRequestDirector.java:293)

E/AndroidRuntime(1132):     at
 org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)

E/AndroidRuntime(1132):     at    
 org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
E/AndroidRuntime(1132):     at 
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
E/AndroidRuntime(1132):     at 
com.example.propertypanther.JSONParser.makeHttpRequest(JSONParser.java:51)

E/AndroidRuntime(1132):     at 
com.example.propertypanther.AddUserActivity$CreateNewUser.doInBackground
(AddUserActivity.java:116)

E/AndroidRuntime(1132):     at  
com.example.propertypanther.AddUserActivity$CreateNewUser.doInBackground
(AddUserActivity.java:1)
E/AndroidRuntime(1132):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
E/AndroidRuntime(1132):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
E/AndroidRuntime(1132):     ... 4 more
I/Choreographer(1132): Skipped 82 frames!  The application may be doing too much work on
its main thread.
I/Choreographer(1132): Skipped 58 frames!  The application may be doing too much work on
its main thread.
E/WindowManager(1132): android.view.WindowLeaked: Activity 
com.example.propertypanther.AddUserActivity has leaked window   
com.android.internal.policy.impl.PhoneWindow$DecorView{b1e3d240 V.E..... R.....ID 0,0- 
729,192} that was originally added here
E/WindowManager(1132):  at android.view.ViewRootImpl.(ViewRootImpl.java:348)
E/WindowManager(1132):  at   
android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
E/WindowManager(1132):  at  
android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
E/WindowManager(1132):  at android.app.Dialog.show(Dialog.java:286)

E/WindowManager(1132):  at 
com.example.propertypanther.AddUserActivity$CreateNewUser.onPreExecute
(AddUserActivity.java:97)

E/WindowManager(1132):  at 
 android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
E/WindowManager(1132):  at android.os.AsyncTask.execute(AsyncTask.java:535)
E/WindowManager(1132):  at   
com.example.propertypanther.AddUserActivity$2.run(AddUserActivity.java:78)
E/WindowManager(1132):  at android.os.Handler.handleCallback(Handler.java:733)
E/WindowManager(1132):  at android.os.Handler.dispatchMessage(Handler.java:95)
E/WindowManager(1132):  at android.os.Looper.loop(Looper.java:136)
E/WindowManager(1132):  at android.app.ActivityThread.main(ActivityThread.java:5017)
E/WindowManager(1132):  at java.lang.reflect.Method.invokeNative(Native Method)
E/WindowManager(1132):  at java.lang.reflect.Method.invoke(Method.java:515)
E/WindowManager(1132):  at  
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
E/WindowManager(1132):  at   
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
E/WindowManager(1132):  at dalvik.system.NativeStart.main(Native Method)

The script files themselves work as far as i am aware – besides, the program never executes the script from what i can tell.

If anyone could help me out, i would really appreciate it! I understand you are all busy people, so thank you so much for taking some time out of your day if you do post ideas 🙂