Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can ajax call be done in Android?

Tags:

ajax

android

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 ?

like image 950
Sanal V Avatar asked Oct 09 '12 05:10

Sanal V


People also ask

Can I use AJAX in Android?

Basically Android is based on java. Then why is not possible to perform AJAX calls.

What is AJAX call used for?

Making Asynchronous Calls: Ajax allows you to make asynchronous calls to a web server. This allows the client browser to avoid waiting for all data to arrive before allowing the user to act once more.

Can I do AJAX call in JavaScript?

Ajax is a programming concept. Below are some ways to make Ajax call in JavaScript. Approach 1: In this approach, we will use the XMLHttpRequest object to make Ajax call. The XMLHttpRequest() method which create XMLHttpRequest object which is used to make request with server.

Is AJAX call blocking?

ajax has async property. If you set it to false it will block. possible duplicate of Synchronous calls with jquery -- you cannot block the runtime without blocking the browser though. And you cannot return the response from the callbacks, you have to assign it to a variable and return that one from the actual function.


4 Answers

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();
    }
}));
like image 75
Phil Avatar answered Oct 11 '22 01:10

Phil


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/

like image 41
Anup Cowkur Avatar answered Oct 11 '22 01:10

Anup Cowkur


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.

like image 34
mattboy Avatar answered Oct 11 '22 01:10

mattboy


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

like image 25
Raghu Nagaraju Avatar answered Oct 11 '22 02:10

Raghu Nagaraju