ProgressDialog cannot stop when load JSON

242 Views Asked by At

please help, I've given up on finding a solution Data json already be in ArrayList, nothing error found but the progressdialog can't stop loading. I'm already put PG.dismis in postExecute but even the Adapter cannot changed.

private static List<DataVoucher> processResponse(String response) {
    List<DataVoucher> list = new ArrayList<DataVoucher>();
    try {
        JSONObject jsonObj = new JSONObject(response);
        JSONArray jsonArray = jsonObj.getJSONArray("produk");
        Log.d(TAG, "data lengt: " + jsonArray.length());
        DataVoucher dataVoucher = null;
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject obj = jsonArray.getJSONObject(i);
            dataVoucher = new DataVoucher();
            dataVoucher.setKode(obj.getString("kode"));
            dataVoucher.setHrg(obj.getString("hrg"));
            dataVoucher.setNom(obj.getString("nom"));
            dataVoucher.setKet(obj.getString("ket"));
            list.add(dataVoucher);

            listvoucher.add(obj.getString("nom"));
        }
    } catch (JSONException e) {
        Log.d(TAG, e.getMessage());
    }
    return list;
}

public static String requestDataVoucher(final String operator) {
    final String TAG = "SEND JSON";
    Thread thread = new Thread() {
        public void run() {
            Looper.prepare();
            JSONObject jsonObjSend = new JSONObject();

            try {
                jsonObjSend.put("type", "svoc");
                jsonObjSend.put("hp", "089631633614");
                jsonObjSend.put("opr", operator);

                Log.i(TAG, jsonObjSend.toString(2));

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

            SendHttpPost(jsonObjSend);
            Looper.loop();
        }
    };
    thread.start();
    return TAG;
}

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

    @Override
    protected void onPreExecute() {
        progressDialog = new ProgressDialog(MainActivity.this);
        progressDialog.setMessage("retrieving...");
        progressDialog.setIndeterminate(false);
        progressDialog.setCancelable(false);
        progressDialog.show();
    }

    @Override
    protected String doInBackground(String... params) {
        String response = requestDataVoucher(pilihOperator
                .getSelectedItem().toString());
        list = processResponse(response);
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        adapter = new ArrayAdapter<String>(MainActivity.this,
                android.R.layout.simple_spinner_item, listvoucher);
        adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        pilihVoucher.setAdapter(adapter);
        adapter.notifyDataSetChanged();

        if (!adapter.isEmpty()) {
            progressDialog.dismiss();
        } // this is annoying
    }
}
2

There are 2 best solutions below

2
On

adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_spinner_item, listvoucher);

I think u mean adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_spinner_item, list);

Btw just dismiss ur progressDialog at onPostExecute() not checking anything and maybe warn the user if the list is empty so you will evade these problems.

0
On

Many issues here

  • doInBackground is running in a thread, different from the ui thread. You are calling requestDataVoucher which is also creating a new thread, useless in this case.

  • You are calling processResponse with the String response which is the result of requestDataVoucher. According to your code, response equals "SEND JSON". So JSONObject jsonObj = new JSONObject(response); will trigger an Exception, listvoucher will remain empty, and so will your adapter. Conclusion, adapter is empty, the progressDialog won't disappear.

I can't propose a fix, as SendHttpPost is unknown. But as far as goes my understanding, you should remove the Thread creation from requestDataVoucher and return the JSON String generate by SendHttpPost.