spinner fatalexception: asynctask #2 执行 doInBackground() 时发生错误

spinner fatalexception: asynctask #2 an error occured while executing doInBackground()

我创建了从服务器获取数据的链式微调器,并通过 json return 它。我的第一个微调器很好。在第二个微调器中,json 给出响应,但是当尝试将 respone 保存在数组中时,由 nullpointerexception 引起的异步任务错误。当我在 log cat 中检查它时,我的 json 响应中没有空值。所以我尝试更改与第一个微调器同名的数组名称没有错误,但第二个微调器显示与第一个微调器相同的值。 我的问题是为什么当数组名称与第一个微调器不同时我的第二个微调器是错误的? nullpointerexception 错误在哪里,因为当我检查它时没有 null ?如何解决?

有我的LoginActivity.java

// Email, password edittext
EditText txtEmail, txtPassword;
private static String KEY_LOGIN = "login";
private String URL_LOGIN = "http://api.omidev.com/login/format/json";
private String URL_PROV = "http://api.omidev.com/lokasi/jenis_lokasi/provinsi/format/json";
private String URL_KOTA = null;
private Spinner spinnerProv, spinnerKota, spinnerKec, spinnerDesa;
// array list for spinner adapter
private ArrayList<Category> categoriesList;
private ArrayList<Category> categoriesListKota;
ProgressDialog pDialog;
HttpPost httppost;
HttpResponse response;
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
ProgressDialog dialog = null;
String mSelected;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    // Session Manager
    session = new SessionManager(getApplicationContext());   
    main = (RelativeLayout) findViewById(R.id.LayoutMain);
    login = (RelativeLayout) findViewById(R.id.LayoutLogin);
    regis = (RelativeLayout) findViewById(R.id.LayoutRegister);

    // Email, Password input text
    txtEmail = (EditText) findViewById(R.id.editText1);
    txtPassword = (EditText) findViewById(R.id.editText2); 
    getActionBar().hide();
    ubahTulisan();
}

private void ubahTulisan()
{
    // Font path
    String fontPath = "fonts/roboto_black.ttf";

    // text view label
    TextView txtMasuk = (TextView) findViewById(R.id.textView1);
    TextView txtAkunBaru = (TextView) findViewById(R.id.TextView01);

    // Loading Font Face
    Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);

    // Applying font
    txtMasuk.setTypeface(tf);
    txtAkunBaru.setTypeface(tf);
}

public void login_click(View view)
{
    main.setVisibility(-5);
    login.setVisibility(1);
    regis.setVisibility(-5);
}

public void regis_click(View view)
{
    main.setVisibility(-5);
    login.setVisibility(-5);
    regis.setVisibility(1);
    categoriesList = new ArrayList<Category>();
    spinnerProv = (Spinner) findViewById(R.id.spinner1);
    spinnerKota = (Spinner) findViewById(R.id.spinner2);
    spinnerKec = (Spinner) findViewById(R.id.Spinner3);
    spinnerDesa = (Spinner) findViewById(R.id.Spinner4);
    // spinner item select listener
    spinnerProv.setOnItemSelectedListener(new OnItemSelectedListener(){

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position,
                long id) {

            try {
                mSelected = URLEncoder.encode(categoriesList.get(position).getId().toString(), "utf-8");
                URL_KOTA ="http://api.omidev.com/lokasi/jenis_lokasi/kota/id/"+mSelected+"/format/json";
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            //new GetKota().execute();
            Toast.makeText(
                    getApplicationContext(),
                            mSelected + " Selected" ,
                    Toast.LENGTH_LONG).show();
            new GetKota().execute();

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }});
    // spinner item select listener
    new GetCategories().execute();
}

public void login_cancel_click(View view)
{
    main.setVisibility(1);
    login.setVisibility(-5);
    regis.setVisibility(-5);
}

public void regis_cancel_click(View view)
{       
    main.setVisibility(1);
    login.setVisibility(-5);
    regis.setVisibility(-5);
}

public void login_accept_click(View view)
{
    main.setVisibility(-5);
    login.setVisibility(1);
    regis.setVisibility(-5);

    new Login().execute();

}

public void regis_accept_click(View view)
{

    main.setVisibility(-5);
    login.setVisibility(1);
    regis.setVisibility(-5);
}

private class Login extends AsyncTask<String, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(LoginActivity.this);
        pDialog.setMessage("Validating User..");
        pDialog.setCancelable(false);
        pDialog.show();

    }

    @Override
    protected Void doInBackground(String... arg) {

        String username = txtEmail.getText().toString();
        String password = txtPassword.getText().toString();
        if(username.trim().length() > 0 && password.trim().length() > 0){
        // Preparing post params
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("email", username));
        params.add(new BasicNameValuePair("password", password));
        ServiceHandler serviceClient = new ServiceHandler();

        String json = serviceClient.makeServiceCall(URL_LOGIN,
                ServiceHandler.POST, params);

        Log.d("Create Response: ", "> " + json);
        if (json != null) {
            try {
                JSONObject jsonObj = new JSONObject(json);
                // checking for error node in json
                if (jsonObj.getString(KEY_LOGIN) != null) { 
                    // new category created successfully
                    JSONArray menuitemArray = jsonObj.getJSONArray("login");
                    for (int i = 0; i < menuitemArray.length(); i++) {
                        String id = menuitemArray.getJSONObject(i).getString("id_user").toString();
                        String email = menuitemArray.getJSONObject(i).getString("email").toString();
                        session.createLoginSession(""+id, ""+email.toString());
                    }
                    // Staring MainActivity
                    Intent a = new Intent(getApplicationContext(), MainActivity.class);
                    startActivity(a);
                    finish();

                } 

            } catch (JSONException e) {
                e.printStackTrace();
                runOnUiThread(new Runnable() {
                    public void run() {
                        alert.showAlertDialog(LoginActivity.this, "Login failed..", "Email Atau Password Salah", false);
                    }
                }); 
            }

        } else {
            Log.e("JSON Data", "Didn't receive any data from server!");
        }
        }else{
            // user didn't entered username or password
            // Show alert asking him to enter the details
            runOnUiThread(new Runnable() {
                public void run() {
            alert.showAlertDialog(LoginActivity.this, "Login failed..", "Masukan Email dan Password", false);
                }
            }); 
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        if (pDialog.isShowing())
            pDialog.dismiss();
    }
}

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}

private void populateSpinner() {
    List<String> lables = new ArrayList<String>();

    for (int i = 0; i < categoriesList.size(); i++) {
        lables.add(categoriesList.get(i).getName());
    }

    // Creating adapter for spinner
    ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, lables);

    // Drop down layout style - list view with radio button
    spinnerAdapter
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    // attaching data adapter to spinner
    spinnerProv.setAdapter(spinnerAdapter);
}

private void populateSpinnerKota() {
    List<String> lables = new ArrayList<String>();

    for (int i = 0; i < categoriesListKota.size(); i++) {
        lables.add(categoriesListKota.get(i).getName());
    }

    // Creating adapter for spinner
    ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, lables);

    // Drop down layout style - list view with radio button
    spinnerAdapter
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    // attaching data adapter to spinner
    spinnerKota.setAdapter(spinnerAdapter);
}

private class GetCategories extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... arg0) {
        ServiceHandler jsonParser = new ServiceHandler();
        String json = jsonParser.makeServiceCall(URL_PROV, ServiceHandler.GET);

        Log.e("Response: ", "> " + json);

        if (json != null) {
            try {
                JSONObject jsonObj = new JSONObject(json);
                if (jsonObj != null) {
                    JSONArray categories = jsonObj
                            .getJSONArray("lokasi");                        

                    for (int i = 0; i < categories.length(); i++) {
                        JSONObject catObj = (JSONObject) categories.get(i);
                        Category cat = new Category(catObj.getString("id_provinsi"),
                                catObj.getString("nama"));
                        categoriesList.add(cat);
                    }
                }

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

        } else {
            Log.e("JSON Data", "Didn't receive any data from server!");
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        populateSpinner();
    }

}

private class GetKota extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... arg0) {
        ServiceHandler jsonParser = new ServiceHandler();
        String json = jsonParser.makeServiceCall(URL_KOTA, ServiceHandler.GET);

        Log.e("Response: ", "> " + json);
        if (json != null) {
            try {
                JSONObject jsonObj = new JSONObject(json);
                if (jsonObj != null) {
                    JSONArray categorieskota = jsonObj
                            .getJSONArray("lokasi");                        

                    for (int i = 0; i < categorieskota.length(); i++) {
                        JSONObject catObj = (JSONObject) categorieskota.get(i);
                        Log.e("Response Kota: ", "> " + catObj.getString("id_kota"));
                        Category catK = new Category(catObj.getString("id_kota"),
                                catObj.getString("nama"));
                        categoriesListKota.add(catK);
                    }
                }

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

        } else {
            Log.e("JSON Data", "Didn't receive any data from server!");
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        populateSpinnerKota();
    }

}

我的category.java

private String id;
private String name;

public Category(){}

public Category(String id, String name){
    this.id = id;
    this.name = name;
}

public void setId(String id){
    this.id = id;
}

public void setName(String name){
    this.name = name;
}

public String getId(){
    return this.id;
}

public String getName(){
    return this.name;
}

我的serviceHandler.java

static InputStream is = null;
static String response = null;
public final static int GET = 1;
public final static int POST = 2;

public ServiceHandler() {

}

/*
 * Making service call
 * @url - url to make request
 * @method - http request method
 * */
public String makeServiceCall(String url, int method) {
    return this.makeServiceCall(url, method, null);
}

/*
 * Making service call
 * @url - url to make request
 * @method - http request method
 * @params - http request params
 * */
public String makeServiceCall(String url, int method,
        List<NameValuePair> params) {
    try {
        // http client
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpEntity httpEntity = null;
        HttpResponse httpResponse = null;

        // Checking http request method type
        if (method == POST) {
            HttpPost httpPost = new HttpPost(url);
            // adding post params
            if (params != null) {
                httpPost.setEntity(new UrlEncodedFormEntity(params));
            }

            httpResponse = httpClient.execute(httpPost);

        } else if (method == GET) {
            // appending params to url
            if (params != null) {
                String paramString = URLEncodedUtils
                        .format(params, "utf-8");
                url += "?" + paramString;
            }
            HttpGet httpGet = new HttpGet(url);

            httpResponse = httpClient.execute(httpGet);

        }
        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, "UTF-8"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        response = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error: " + e.toString());
    }

    return response;

}

这是 Logcat 错误:

02-05 18:20:48.953: E/Response:(29903): > {"lokasi":[{"id_provinsi":"1","nama":"Bali"},{"id_provinsi":"2","nama":"Banten"},{"id_provinsi":"3","nama":"Bengkulu"},{"id_provinsi":"4","nama":"DI Yogyakarta"},{"id_provinsi":"5","nama":"DKI Jakarta"},{"id_provinsi":"6","nama":"Gorontalo"},{"id_provinsi":"7","nama":"Irian Jaya Barat"},{"id_provinsi":"8","nama":"Jambi"},{"id_provinsi":"9","nama":"Jawa Barat"},{"id_provinsi":"10","nama":"Jawa Tengah"},{"id_provinsi":"11","nama":"Jawa Timur"},{"id_provinsi":"12","nama":"Kalimantan Barat"},{"id_provinsi":"13","nama":"Kalimantan Selatan"},{"id_provinsi":"14","nama":"Kalimantan Tengah"},{"id_provinsi":"15","nama":"Kalimantan Timur"},{"id_provinsi":"16","nama":"Kep. Bangka Belitung"},{"id_provinsi":"17","nama":"Kep. Riau"},{"id_provinsi":"18","nama":"Lampung"},{"id_provinsi":"19","nama":"Maluku"},{"id_provinsi":"20","nama":"Maluku Utara"},{"id_provinsi":"21","nama":"Nanggroe Aceh Darussalaam"},{"id_provinsi":"22","nama":"Nusa Tenggara Barat"},{"id_provinsi":"23","nama":"Nusa Tenggara Timur"},{"id_provinsi":"24","nama":"Papua"},{"id_provinsi":"25","nama":"Riau"},{"id_provinsi":"26","nama":"Sulawesi Selatan"},{"id_provinsi":"27","nama":"Sulawesi Tengah"},{"id_provinsi":"28","nama":"Sulawesi Tenggara"},{"id_provinsi":"29","nama":"Sulawesi Utara"},{"id_provinsi":"30","nama":"Sumatra Barat"},{"id_provinsi":"31","nama":"Sumatra Selatan"},{"id_provinsi":"32","nama":"Sumatra Utara"}]}
02-05 18:20:49.573: E/Response:(29903): > {"lokasi":[{"id_kota":"1","id_provinsi":"1","nama":"KABUPATEN BADUNG"},{"id_kota":"2","id_provinsi":"1","nama":"KABUPATEN BANGLI"},{"id_kota":"3","id_provinsi":"1","nama":"KABUPATEN BULELENG"},{"id_kota":"4","id_provinsi":"1","nama":"KABUPATEN GIANYAR"},{"id_kota":"5","id_provinsi":"1","nama":"KABUPATEN JEMBRANA"},{"id_kota":"6","id_provinsi":"1","nama":"KABUPATEN KARANG ASEM"},{"id_kota":"7","id_provinsi":"1","nama":"KABUPATEN KLUNGKUNG"},{"id_kota":"8","id_provinsi":"1","nama":"KABUPATEN TABANAN"},{"id_kota":"9","id_provinsi":"1","nama":"KOTA DENPASAR"}]}
02-05 18:20:49.573: E/Response Kota:(29903): > 1
02-05 18:20:49.573: W/dalvikvm(29903): threadid=16: thread exiting with uncaught exception (group=0x415b1360)
02-05 18:20:49.603: E/AndroidRuntime(29903): FATAL EXCEPTION: AsyncTask #2
02-05 18:20:49.603: E/AndroidRuntime(29903): java.lang.RuntimeException: An error occured while executing doInBackground()
02-05 18:20:49.603: E/AndroidRuntime(29903):    at android.os.AsyncTask.done(AsyncTask.java)
02-05 18:20:49.603: E/AndroidRuntime(29903):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
02-05 18:20:49.603: E/AndroidRuntime(29903):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
02-05 18:20:49.603: E/AndroidRuntime(29903):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
02-05 18:20:49.603: E/AndroidRuntime(29903):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-05 18:20:49.603: E/AndroidRuntime(29903):    at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java)
02-05 18:20:49.603: E/AndroidRuntime(29903):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
02-05 18:20:49.603: E/AndroidRuntime(29903):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
02-05 18:20:49.603: E/AndroidRuntime(29903):    at java.lang.Thread.run(Thread.java:856)
02-05 18:20:49.603: E/AndroidRuntime(29903): Caused by: java.lang.NullPointerException
02-05 18:20:49.603: E/AndroidRuntime(29903):    at com.android.desaku.LoginActivity$GetKota.doInBackground(LoginActivity.java:377)
02-05 18:20:49.603: E/AndroidRuntime(29903):    at com.android.desaku.LoginActivity$GetKota.doInBackground(LoginActivity.java:1)
02-05 18:20:49.603: E/AndroidRuntime(29903):    at android.os.AsyncTask.call(AsyncTask.java)
02-05 18:20:49.603: E/AndroidRuntime(29903):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
02-05 18:20:49.603: E/AndroidRuntime(29903):    ... 5 more

请帮助我谢谢。抱歉,我不知道如何 post logcat

尝试更改这些代码:

private static String KEY_LOGIN = "login";
private String URL_LOGIN = "http://api.omidev.com/login/format/json";
private String URL_PROV = "http://api.omidev.com/lokasi/jenis_lokasi/provinsi/format/json";

至:

private static final String KEY_LOGIN = "login";
private static final String URL_LOGIN = "http://api.omidev.com/login/format/json";
private static final String URL_PROV = "http://api.omidev.com/lokasi/jenis_lokasi/provinsi/format/json";

看看区别。第二个使用 static final。然后,在使用 String 参数的同时将 void 更改为 String。例如:

private class Login extends AsyncTask<String, Void, Void> {

// Change void to String if the first AsyncTask parameter is a String.
// If the first parameter is Void, change to void (in lowercase)
@Override
protected String doInBackground(String... arg) { // Must be a String type, because the first parameter type is a String.

对不起我的错。我已经修复了我的代码 在 GetKota Asynctask 中我没有初始化 categoriesListKota,所以它是由 nullpointerexception 引起的错误。 要在第一次旋转的选定值时进行第二次旋转更改,我添加 notifyDataSetChanged() 以更改 populateSpinnerKota() 上的微调器适配器和 GetKota Asynctask 上的 clear() 以使微调器从以前的数据中清除.

这是 LoginActivity.java

的正确代码
// Alert Dialog Manager
AlertDialogManager alert = new AlertDialogManager();

// Session Manager Class
SessionManager session;

// Email, password edittext
EditText txtEmail, txtPassword;
private static String KEY_LOGIN = "login";
private String URL_LOGIN = "http://api.omidev.com/login/format/json";
private String URL_PROV = "http://api.omidev.com/lokasi/jenis_lokasi/provinsi/format/json";
private String URL_KOTA = null;
private Spinner spinnerProv, spinnerKota, spinnerKec, spinnerDesa;
// array list for spinner adapter
private ArrayList<Category> categoriesList = new ArrayList<Category>();
private ArrayList<Category> categoriesListKota = new ArrayList<Category>();
ProgressDialog pDialog;
HttpPost httppost;
HttpResponse response;
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
ProgressDialog dialog = null;
String mSelected;
List<String> list = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    // Session Manager
    session = new SessionManager(getApplicationContext());   
    main = (RelativeLayout) findViewById(R.id.LayoutMain);
    login = (RelativeLayout) findViewById(R.id.LayoutLogin);
    regis = (RelativeLayout) findViewById(R.id.LayoutRegister);

    // Email, Password input text
    txtEmail = (EditText) findViewById(R.id.editText1);
    txtPassword = (EditText) findViewById(R.id.editText2); 
    getActionBar().hide();
    ubahTulisan();
}

private void ubahTulisan()
{
    // Font path
    String fontPath = "fonts/roboto_black.ttf";

    // text view label
    TextView txtMasuk = (TextView) findViewById(R.id.textView1);
    TextView txtAkunBaru = (TextView) findViewById(R.id.TextView01);

    // Loading Font Face
    Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);

    // Applying font
    txtMasuk.setTypeface(tf);
    txtAkunBaru.setTypeface(tf);
}

public void login_click(View view)
{
    main.setVisibility(-5);
    login.setVisibility(1);
    regis.setVisibility(-5);
}

public void regis_click(View view)
{
    main.setVisibility(-5);
    login.setVisibility(-5);
    regis.setVisibility(1);

    spinnerProv = (Spinner) findViewById(R.id.spinner1);
    spinnerKota = (Spinner) findViewById(R.id.spinner2);
    spinnerKec = (Spinner) findViewById(R.id.Spinner3);
    spinnerDesa = (Spinner) findViewById(R.id.Spinner4);
    // spinner item select listener
    spinnerProv.setOnItemSelectedListener(new OnItemSelectedListener(){

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position,
                long id) {

            try {
                mSelected = URLEncoder.encode(categoriesList.get(position).getId().toString(), "utf-8");
                URL_KOTA ="http://api.omidev.com/lokasi/jenis_lokasi/kota/id/"+mSelected+"/format/json";
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            Toast.makeText(
                    getApplicationContext(),
                            mSelected + " Selected" ,
                    Toast.LENGTH_LONG).show();
            new GetKota().execute();

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }});
    // spinner item select listener
    new GetCategories().execute();
}

public void login_cancel_click(View view)
{
    main.setVisibility(1);
    login.setVisibility(-5);
    regis.setVisibility(-5);
}

public void regis_cancel_click(View view)
{       
    main.setVisibility(1);
    login.setVisibility(-5);
    regis.setVisibility(-5);
}

public void login_accept_click(View view)
{
    main.setVisibility(-5);
    login.setVisibility(1);
    regis.setVisibility(-5);

    new Login().execute();

}

public void regis_accept_click(View view)
{

    main.setVisibility(-5);
    login.setVisibility(1);
    regis.setVisibility(-5);
}

private class Login extends AsyncTask<String, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(LoginActivity.this);
        pDialog.setMessage("Validating User..");
        pDialog.setCancelable(false);
        pDialog.show();

    }

    @Override
    protected Void doInBackground(String... arg) {

        String username = txtEmail.getText().toString();
        String password = txtPassword.getText().toString();
        if(username.trim().length() > 0 && password.trim().length() > 0){
        // Preparing post params
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("email", username));
        params.add(new BasicNameValuePair("password", password));
        ServiceHandler serviceClient = new ServiceHandler();

        String json = serviceClient.makeServiceCall(URL_LOGIN,
                ServiceHandler.POST, params);

        Log.d("Create Response: ", "> " + json);
        if (json != null) {
            try {
                JSONObject jsonObj = new JSONObject(json);
                // checking for error node in json
                if (jsonObj.getString(KEY_LOGIN) != null) { 
                    // new category created successfully
                    JSONArray menuitemArray = jsonObj.getJSONArray("login");
                    for (int i = 0; i < menuitemArray.length(); i++) {
                        String id = menuitemArray.getJSONObject(i).getString("id_user").toString();
                        String email = menuitemArray.getJSONObject(i).getString("email").toString();
                        session.createLoginSession(""+id, ""+email.toString());
                    }
                    // Staring MainActivity
                    Intent a = new Intent(getApplicationContext(), MainActivity.class);
                    startActivity(a);
                    finish();

                } 

            } catch (JSONException e) {
                e.printStackTrace();
                runOnUiThread(new Runnable() {
                    public void run() {
                        alert.showAlertDialog(LoginActivity.this, "Login failed..", "Email Atau Password Salah", false);
                    }
                }); 
            }

        } else {
            Log.e("JSON Data", "Didn't receive any data from server!");
        }
        }else{
            // user didn't entered username or password
            // Show alert asking him to enter the details
            runOnUiThread(new Runnable() {
                public void run() {
            alert.showAlertDialog(LoginActivity.this, "Login failed..", "Masukan Email dan Password", false);
                }
            }); 
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        if (pDialog.isShowing())
            pDialog.dismiss();
    }
}

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}

private void populateSpinner() {
    List<String> lables = new ArrayList<String>();

    for (int i = 0; i < categoriesList.size(); i++) {
        lables.add(categoriesList.get(i).getName());
    }

    // Creating adapter for spinner
    ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, lables);

    // Drop down layout style - list view with radio button
    spinnerAdapter
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    // attaching data adapter to spinner
    spinnerProv.setAdapter(spinnerAdapter);
}

private void populateSpinnerKota() {
    List<String> lables = new ArrayList<String>();

    for (int i = 0; i < categoriesListKota.size(); i++) {
        lables.add(categoriesListKota.get(i).getName());
    }

    // Creating adapter for spinner
    ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, lables);
    spinnerAdapter.notifyDataSetChanged();
    // Drop down layout style - list view with radio button
    spinnerAdapter
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    // attaching data adapter to spinner
    spinnerKota.setAdapter(spinnerAdapter);
}

private class GetCategories extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... arg0) {
        ServiceHandler jsonParser = new ServiceHandler();
        String json = jsonParser.makeServiceCall(URL_PROV, ServiceHandler.GET);

        Log.e("Response: ", "> " + json);

        if (json != null) {
            try {
                JSONObject jsonObj = new JSONObject(json);
                if (jsonObj != null) {
                    JSONArray categories = jsonObj
                            .getJSONArray("lokasi");                        

                    for (int i = 0; i < categories.length(); i++) {
                        JSONObject catObj = (JSONObject) categories.get(i);
                        Category cat = new Category(catObj.getString("id_provinsi"),
                                catObj.getString("nama"));
                        categoriesList.add(cat);
                    }
                }

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

        } else {
            Log.e("JSON Data", "Didn't receive any data from server!");
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        populateSpinner();
    }

}

private class GetKota extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... arg0) {
        ServiceHandler jsonParser = new ServiceHandler();
        String json = jsonParser.makeServiceCall(URL_KOTA, ServiceHandler.GET);
        categoriesListKota.clear();
        Log.e("Response: ", "> " + json);
        if (json != null) {
            try {
                JSONObject jsonObj = new JSONObject(json);
                if (jsonObj != null) {
                    JSONArray categorieskota = jsonObj
                            .getJSONArray("lokasi");                        

                    for (int i = 0; i < categorieskota.length(); i++) {
                        JSONObject catObj = (JSONObject) categorieskota.get(i);                         
                        Category catK = new Category(catObj.getString("id_kota"),
                                catObj.getString("nama"));
                        categoriesListKota.add(catK);
                    }
                }

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

        } else {
            Log.e("JSON Data", "Didn't receive any data from server!");
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        populateSpinnerKota();
    }

}