How to Print Message when Json Response has no fileds?

倾然丶 夕夏残阳落幕 提交于 2020-01-06 13:50:09

问题


I am using this tutorial http://www.androidhive.info/2012/01/android-json-parsing-tutorial/ for json parsing,and in my app response and everything works fine,but what if response has no filed,suppose as per this tutorial if it has reponse like {"contacts":""},it means no array so how to print toast if no fields there.


回答1:


if (response=null)
Toast.maketext(this,"No response",Toast.lenght_long).show();



回答2:


// Getting JSON Array node
contacts = jsonObj.getJSONArray(TAG_CONTACTS);
if(contacts == null || contacts.length() == 0){
    // Do your thing..
}



回答3:


You can try to check if your response is null or with length 0

if (response=null || response.length == 0 )
Toast.makeText(this,"No contacts found.",Toast.LENGTH_LONG).show();



回答4:


Try this as per example code

// Getting JSON Array node
contacts = jsonObj.getJSONArray(TAG_CONTACTS);
if(contacts == null || contacts.length() == 0){
   // show toast in doinbackground using runOnUiThread
   runOnUiThread(new Runnable() {

                    public void run() {

                      Toast.makeText(getApplicationContext(), "No response", Toast.LENGTH_SHORT).show();

                       }
                    });
}



回答5:


as per this example try this..

protected void onPostExecute(Void result) {
    super.onPostExecute(result);

    if(contacts == null || contacts.length() == 0){                
         Toast.makeText(getApplicationContext(), "No Data",Toast.LENGTH_SHORT).show();       
    }

    // Dismiss the progress dialog
    if (pDialog.isShowing())
         pDialog.dismiss();          
}


来源:https://stackoverflow.com/questions/27062720/how-to-print-message-when-json-response-has-no-fileds

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!