AndroidBoy AndroidBoy - 4 months ago 18
Android Question

How to display Alert/Toast While my Response is NULL

The Parameter result can be null means i want to display Alert/Toast how can i do this, because my App going to be crashed when the Response is NULL.Here we go what i have thy this means.I think NULL value problem so i want to avoid this thing (crashed)

private class BackTask extends AsyncTask<Void,Void,Void> {
ArrayList<String> list;
protected void onPreExecute(){
super.onPreExecute();
list=new ArrayList<>();
}
protected Void doInBackground(Void...params){
InputStream is=null;
String result="";
try{
HttpClient httpclient=new DefaultHttpClient();
HttpPost httppost= new HttpPost("http://IP/web/getOrderPendCusname.php");
HttpResponse response=httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
// Get our response as a String.
is = entity.getContent();
System.out.println(is);
}catch(IOException e){
e.printStackTrace();
}

//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"));
String line = null;
while ((line = reader.readLine()) != null) {
result+=line;
}
is.close();
//result=sb.toString();
}catch(Exception e){
e.printStackTrace();
}
// parse json data
try{
JSONObject object = new JSONObject(result);
JSONArray jArray = object.getJSONArray("Cus_name");
//JSONArray jArray =new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject jsonObject = jArray.getJSONObject(i);
// add interviewee name to arraylist
list.add(jsonObject.getString("Cus_name"));
}
}
catch(JSONException e){
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result){
listItems.addAll(list);
adapter.notifyDataSetChanged();
}
}


LOGCAT:

org.json.JSONException: Value null of type org.json.JSONObject$1 cannot be converted to JSONObject
07-21 17:01:16.057 17179-17289/com.example.vari.new_varri W/System.err: at org.json.JSON.typeMismatch(JSON.java:111)
07-21 17:01:16.057 17179-17289/com.example.vari.new_varri W/System.err: at org.json.JSONObject.<init>(JSONObject.java:160)
07-21 17:01:16.057 17179-17289/com.example.vari.new_varri W/System.err: at org.json.JSONObject.<init>(JSONObject.java:173)
07-21 17:01:16.057 17179-17289/com.example.vari.new_varri W/System.err: at com.example.vari.new_varri.Order_Pending$BackTask.doInBackground(Order_Pending.java:579)
07-21 17:01:16.058 17179-17289/com.example.vari.new_varri W/System.err: at com.example.vari.new_varri.Order_Pending$BackTask.doInBackground(Order_Pending.java:538)
07-21 17:01:16.058 17179-17289/com.example.vari.new_varri W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:295)
07-21 17:01:16.058 17179-17289/com.example.vari.new_varri W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-21 17:01:16.058 17179-17289/com.example.vari.new_varri W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
07-21 17:01:16.058 17179-17289/com.example.vari.new_varri W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
07-21 17:01:16.058 17179-17289/com.example.vari.new_varri W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
07-21 17:01:16.058 17179-17289/com.example.vari.new_varri W/System.err: at java.lang.Thread.run(Thread.java:818)

Answer

Try this,

 if(ReceivedVal == null)
 {
   Toast.makeText(context, "Null value Received..", Toast.LENGTH_SHORT).show();
   Log.i("NULL" , "Null value received");
 }

EDIT 1 :

 runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        Toast.makeText(context, "Null value Received..", Toast.LENGTH_SHORT).show();
                    }
                });
Comments