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.
Asked
Active
Viewed 879 times
5 Answers
0
if (response=null)
Toast.maketext(this,"No response",Toast.lenght_long).show();

Zied R.
- 4,964
- 2
- 36
- 67

therealprashant
- 701
- 15
- 27
-
check example and tell me where can i add this line – Nov 21 '14 at 13:46
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();

Marcel Krivek
- 167
- 8

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
-
-
-
You will have to make your toast the UIThread, in this case onPostExecute – Carnal Nov 21 '14 at 14:00
-
I gave you a hint, you can figure the rest out! You could make doInBackground to return a list with contacts or a Boolean indicating if there are any contacts. And act on that onPostExecute – Carnal Nov 21 '14 at 14:08
-
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