如果JSON响应为空,如何显示消息/ Toast

时间:2018-04-03 15:36:06

标签: android json api

我正在从Android应用程序中搜索api,当搜索没有从api返回结果时,我想向用户显示Toast /错误消息。

当api没有返回结果时,会在日志中显示以下内容:

{boards: null}

这是我想要显示消息/吐司的地方。

我试过了:

if (name.equals ("null");

我发现了许多其他“解决方案”,但似乎都没有。

请参阅以下代码:

public class apitestsearch extends AppCompatActivity {

    EditText boardName;
    TextView resultView;

    public void findBoard (View view){
        // Log.i("board", boardName.getText().toString());

        InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        mgr.hideSoftInputFromWindow(boardName.getWindowToken(), 0);

        DownloadTask task = new DownloadTask();
        task.execute("https://www.api.com/airquality/api/json.php?boardID="+ boardName.getText().toString());
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apitestsearch);


      boardName = (EditText)findViewById(R.id.boardName);
        resultView = (TextView) findViewById(R.id.resultView);

    }

    public class DownloadTask extends AsyncTask<String, Void, String>{

        @Override
        protected String doInBackground(String... urls) {

            String result = "";
                URL url;
            HttpURLConnection urlConnection = null;

            try {
                url = new URL(urls[0]);

                urlConnection = (HttpURLConnection) url.openConnection();

                InputStream in = urlConnection.getInputStream();

                InputStreamReader reader = new InputStreamReader(in);

                int data = reader.read();

                while (data != -1)
                {
                    char current = (char) data;

                    result += current;

                    data = reader.read();
                }

                return result;

            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "No results found", Toast.LENGTH_SHORT);
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);

            try {
                String message = " ";
                JSONObject jsonObject = new JSONObject(result);

                String board = jsonObject.getString("boards");

           //     Log.i("boardID", board);
                JSONArray arr = new JSONArray(board);

                for(int i = 0; i < 1; i++)
                {
                    JSONObject jsonPart = arr.getJSONObject(i);

                    String name = "";
                    name = jsonPart.getString("boardID");

                    if(name != ""){
                        message += name + "\r\n";
                    }
                }

                if (message != "") {
                    resultView.setText(message);
                }


            } catch (JSONException e) {
                Toast.makeText(getApplicationContext(), "No results found", Toast.LENGTH_SHORT);
            }
          //  Log.i("Content", result);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

你可以试试这个。

board

如果您想全局使用String board; if(jsonObject.isNull("boards")){ // null handling goes here.. }else{ board = jsonObject.getString("boards"); } ,那么

String board;
if(!jsonObject.isNull("boards")){
  board = jsonObject.getString("boards");
}

更短

{{1}}

答案 1 :(得分:0)

String board = "Not available";
//null check
if(!jsonObject.isNull("boards")){
  board = jsonObject.getString("boards");
}else{
  // your toast 
  Toast.makeText(context, "Board " + board, Toast.LENGTH_SHORT).show();
}