0

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.

5 Answers5

0
if (response=null)
Toast.maketext(this,"No response",Toast.lenght_long).show();
Zied R.
  • 4,964
  • 2
  • 36
  • 67
therealprashant
  • 701
  • 15
  • 27
0

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();
Benjamin RD
  • 11,516
  • 14
  • 87
  • 157
0
// Getting JSON Array node
contacts = jsonObj.getJSONArray(TAG_CONTACTS);
if(contacts == null || contacts.length() == 0){
    // Do your thing..
}
Carnal
  • 21,744
  • 6
  • 60
  • 75
0

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();

                       }
                    });
}
Furqan
  • 1,417
  • 1
  • 15
  • 22
  • Display `Toast` in `doInBackground` refer this link [http://stackoverflow.com/questions/13790351/how-to-show-toast-in-asynctask-in-doinbackground](http://stackoverflow.com/questions/13790351/how-to-show-toast-in-asynctask-in-doinbackground) – Furqan Nov 22 '14 at 05:04
0

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();          
}
codePG
  • 1,754
  • 1
  • 12
  • 19