可以在Android中进行ajax调用吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12793544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 12:06:08  来源:igfitidea点击:

Can ajax call be done in Android?

androidajax

提问by Sanal V

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 web server. But for that I've to make request simultaneously with typing each letter. But when I googled, I got mixed reactions. Some people said Ajax call is not possible in Android. Basically Android is based on java. Then why is not possible to perform AJAX calls. Could anyone guide me to a good link related to AJAX call in Android if it is possible ?

我正在开发一个应用程序,其中有搜索选项。在那个搜索框中,如果我输入“a”,我希望我所有朋友的名字都以 a 开头,我将从 Web 服务器获取。但为此,我必须在输入每个字母的同时提出请求。但是当我用谷歌搜索时,我得到了不同的反应。有人说Ajax 调用在Android 中是不可能的。基本上Android是基于java的。那么为什么不能执行 AJAX 调用。如果可能的话,有人可以指导我找到与 Android 中的 AJAX 调用相关的良好链接吗?

回答by Phil

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:

您可以使用droidQuery,这是jQuery的的的Android端口,包括大部分的功能和语法jQuery的,包括阿贾克斯。例如:

$.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();
    }
}));

回答by mattboy

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.

我所知道的最接近的是使用 AutoCompleteTextView。您需要为它制作一个自定义适配器,以便在用户键入任何内容时调用 Web 服务器并基于此返回过滤器结果。

Here's an example.

这是一个例子

回答by Raghu Nagaraju

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

在加载屏幕时使用 asynctask 从服务器获取名称。然后您可以使用 AutoCompleteTextView 或 MultiAutoCompleteTextView 来满足您的需求。

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

您在适配器中指定已提取的名称。查看更多关于AutoCompleteTextView

and MultiAutoCompleteTextView

MultiAutoCompleteTextView