Can ajax call be done in Android?

前端 未结 4 383
栀梦
栀梦 2020-12-11 03:51

I\'m developing an application in which I\'ve got search option. In that search box, if I type \'a\' I want all names of all my friends starting with a, which I\'ll get from

相关标签:
4条回答
  • 2020-12-11 04:05

    Yes it is possible, but with a few conditions and restrictions.

    Check out these resources for more info:

    Can you use AJAX calls with Android?

    Android: Implication of using AsyncTask to make repeated Ajax Calls

    https://developer.android.com/guide/topics/search/search-dialog.html

    http://www.grokkingandroid.com/android-tutorial-adding-search-to-your-apps/

    0 讨论(0)
  • 2020-12-11 04:06

    Fetch names from the server on loading the screen, using asynctask. Then you can make use of AutoCompleteTextView or MultiAutoCompleteTextView to achieve your need.

    You specify already fetched names in the adapter. See more on AutoCompleteTextView

    and MultiAutoCompleteTextView

    0 讨论(0)
  • 2020-12-11 04:08

    You can use droidQuery, which is The Android port of jQuery, and includes most of the features and syntax of jQuery, including Ajax. For example:

    $.ajax(new AjaxOptions().url("http://www.example.com").type("GET").dataType("json").success(new Function() {
        @Override
        public void invoke($ d, Object... args) {
            JSONObject json = (JSONObject) args[0];
            //TODO handle json. If expecting a JSONArray, just cast args[0] to JSONArray.
        }
    }).error(new Function() {
        @Override
        public void invoke($ d, Object... args) {
            AjaxError error = (AjaxError) args[0];
            Toast.makeText(MyActivity.this, "Error (" + error.status + "): " + error.reason, Toast.LENGTH_LONG).show();
        }
    }));
    
    0 讨论(0)
  • 2020-12-11 04:26

    Closest I know is using an AutoCompleteTextView. You will need to make a custom adapter for it that makes calls to the web server whenever a user types anything and returns filter results based on that.

    Here's an example.

    0 讨论(0)
提交回复
热议问题