簡體   English   中英

從json解析數據為空或沒有結果時如何舉杯?

[英]How to make a toast when parsing data from json is null or no result?

當我的表數據庫基於解析json為空時,我會舉杯敬酒。 我的意思是,當沒有結果或找不到數據時,敬酒“對不起,找不到數據”。 任何幫助將不勝感激。

這是我用於顯示數據的代碼

private void showEmployee(){
    JSONObject jsonObject = null;
    ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>();
    try {
        jsonObject = new JSONObject(JSON_STRING);
        JSONArray result = jsonObject.getJSONArray(konfigurasi.TAG_JSON_ARRAY);

        for(int i = 0; i<result.length(); i++){
            JSONObject jo = result.getJSONObject(i);
            String id = jo.getString(konfigurasi.TAG_ID);
            String nama = jo.getString(konfigurasi.TAG_NAMA);
            String pyg = jo.getString(konfigurasi.TAG_PENYELENGGARA);
            String tmpt = jo.getString(konfigurasi.TAG_TEMPAT);
            String tgl = jo.getString(konfigurasi.TAG_TGL);
            String jam = jo.getString(konfigurasi.TAG_JAM);
            String email = jo.getString(konfigurasi.TAG_EMAIL);

            HashMap<String,String> employees = new HashMap<>();
            employees.put(konfigurasi.TAG_ID,id);
            employees.put(konfigurasi.TAG_NAMA,nama);
            employees.put(konfigurasi.TAG_PENYELENGGARA,pyg);
            employees.put(konfigurasi.TAG_TEMPAT,tmpt);
            employees.put(konfigurasi.TAG_TGL,tgl);
            employees.put(konfigurasi.TAG_JAM,jam);
            employees.put(konfigurasi.TAG_EMAIL,email);
            list.add(employees);
        }

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

    ListAdapter adapter = new MySimpleArrayAdapter(this, list);

    listView.setAdapter(adapter);
}

這是postexcetude代碼:

private void getJSON(){
class GetJSON extends AsyncTask<Void,Void,String>{

    ProgressDialog loading;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        loading = ProgressDialog.show(TampilSemuaPgw.this,"Mengambil Data","Mohon Tunggu...",false,false);
    }

    @Override
    protected void onPostExecute(String s) {

        super.onPostExecute(s);
        if(s == null || s.length() == 0){
            Toast.makeText(getApplicationContext(), "No Data",Toast.LENGTH_LONG).show();
        }

        // Dismiss the progress dialog
        if (loading.isShowing())
            loading.dismiss();
        JSON_STRING = s;
        showEmployee();
    }

    @Override
    protected String doInBackground(Void... params) {
        RequestHandler rh = new RequestHandler();
        String s = rh.sendGetRequest(konfigurasi.URL_GET_ALL);

        return s;
    }
}
GetJSON gj = new GetJSON();
gj.execute();

}

KeLiuyue的更新代碼

    @Override
protected String doInBackground(Void... params) {
    RequestHandler rh = new RequestHandler();
    String s = rh.sendGetRequest(konfigurasi.URL_GET_ALL);
    return s;
}

@Override
protected void onPostExecute(String s) {

    try {
        JSONObject jsonObject = new JSONObject(s);
        JSONArray jsonArray = jsonObject.getJSONArray("result");
        if(jsonArray.length() == 0){
            Toast.makeText(getApplicationContext(), "No Data", Toast.LENGTH_LONG).show();
            if (loading.isShowing()){
               loading.dismiss();
            }
            return;
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    Log.e("TAG",s);
    // Dismiss the progress dialog
    if (loading.isShowing())
        loading.dismiss();
    JSON_STRING = s;
    showEmployee();
}

在您的代碼中嘗試一下。

 @Override
protected String doInBackground(Void... params) {
    RequestHandler rh = new RequestHandler();
    String s = rh.sendGetRequest(konfigurasi.URL_GET_ALL);
    return s;
}

@Override
protected void onPostExecute(String s) {

    // edited here
    try {
        JSONObject jsonObject = new JSONObject(s);
        JSONArray jsonArray = jsonObject.getJSONArray("result");
        if(jsonArray.length() == 0){
            Toast.makeText(getApplicationContext(), "No Data", Toast.LENGTH_LONG).show();
            if (loading.isShowing()){
               loading.dismiss();
            }
            return;
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    Log.e("TAG",s);
    // Dismiss the progress dialog
    if (loading.isShowing())
        loading.dismiss();
    JSON_STRING = s;
    showEmployee();
}

1.在doInBackground方法中確定返回值是否為空

2.在onPostExecute方法中確定參數值是否為空

您需要調試此問題,為什么您的吐司不顯示:

您已正確將show Toast代碼放入onPostExecute

現在進行調試,首先放置一個Log來知道s的值,無論它是null還是空。

如果是,仍然沒有顯示Toast,請在Toast之前將對話框關閉對話框移開並進行檢查。

如果Toast仍然不顯示調試調試showEmployee()方法的功能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM