参数结果可以为null意味着我想显示Alert / Toast我怎么能这样做,因为我的应用程序将在响应为NULL时崩溃。这就是我有你的意思。我认为NULL值问题所以我想避免这件事(坠毁)
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)
答案 0 :(得分:1)
你真的可以在google上找到它。
if(IDontKnowWhatValue == null){
Toast.makeText(context, "MyMessage", Toast.LENGTH_SHORT).show();
}
答案 1 :(得分:1)
试试这个,
if(ReceivedVal == null)
{
Toast.makeText(context, "Null value Received..", Toast.LENGTH_SHORT).show();
Log.i("NULL" , "Null value received");
}
编辑1:
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText(context, "Null value Received..", Toast.LENGTH_SHORT).show();
}
});
编辑2
您的result
变量是字符串,因此您可以将equals()
视为
if(result.equals(""))
{
// your toast is here
}
else
{
// do your stuff here
}