问题
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