Android 当解析来自json的数据为空或没有结果时,如何制作toast?
基于json解析,当我的表数据库为空时,我有一个小问题。我的意思是,当数据没有结果或没有找到数据时,干杯“对不起,没有找到数据”。任何帮助都将不胜感激 这里是显示数据的代码Android 当解析来自json的数据为空或没有结果时,如何制作toast?,android,arrays,json,Android,Arrays,Json,基于json解析,当我的表数据库为空时,我有一个小问题。我的意思是,当数据没有结果或没有找到数据时,干杯“对不起,没有找到数据”。任何帮助都将不胜感激 这里是显示数据的代码 private void showEmployee(){ JSONObject jsonObject = null; ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String&
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);
}
在你的代码中试试这个
@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
方法中的参数值是否为空您需要调试此问题,为什么您的toast没有显示:
您已将ShowToast代码正确地放入onPostExecute中
现在进行调试,首先放置一个日志以了解s的值,无论它是null还是空的
如果是,但仍未显示toast,请在toast之前移动对话框Disclose对话框并选中
如果Toast仍然没有像它那样显示调试showEmployee()方法。是否检查showEmployee()是否返回某些内容?是否确定从doInBackground返回的字符串为null或空?我不确定@NabinHandari,那么,您对我的doInBackground代码有何建议?我只是想在没有数据显示的情况下干杯或参加另一个活动。请帮助,谢谢看起来好像有消息来自服务器,即使没有数据。尝试记录从服务器接收的数据。您可以重试。我拿走了一些东西@RizkiDeddySusanto@NabinBhandari我删除了它。仍然没有发生任何事情您的
JSON\u字符串中有数据吗?您的设备中是否显示了ListView
?是的,我在ListView中显示了JSON。。我的意思是当JSON_字符串没有数据时,则显示或吐司类似“没有数据”的内容。。
@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();
}