简体   繁体   English

JSON响应为空时如何显示消息/祝酒

[英]How to display a message/toast if JSON response is null

I'm searching an api from an android app and I would like to display a Toast/error message to the user when the search returns no results from the api. 我正在从Android应用中搜索一个api,当搜索未从api返回任何结果时,我想向用户显示Toast /错误消息。

When the api returns no results the following is displayed to the logs: 当api返回任何结果时,日志中将显示以下内容:

{boards: null}

This is where I would like to display a message/toast. 这是我要显示消息/敬酒的地方。

I have tried: 我努力了:

if (name.equals ("null");

also many other 'solutions' that i've found, but none seem to work. 我发现了许多其他“解决方案”,但似乎都没有用。

please see code below: 请参见下面的代码:

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);
        }
    }
}

You can try this. 你可以试试看

if(jsonObject.isNull("boards")){
// null handling goes here..
}else{
   String board = jsonObject.getString("boards");
}

and if you want to use board as globally then 如果要在全球范围内使用board

String board;    
if(jsonObject.isNull("boards")){
// null handling goes here..
}else{
board = jsonObject.getString("boards");
}

and more short 还有更短

String board;
if(!jsonObject.isNull("boards")){
  board = jsonObject.getString("boards");
}
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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM