5

I want to create a content provider that will bring results from a web server. Its like I send some post parameters to my web server and it returns me with some JSON response which I must parse and show it in the Quick Search Box of android.

I already looked into SearchableDictionary but they don't seem to be connecting to a server to get results. I searched over the web but no proper examples anywhere.

How can I do it?

Sudarshan Bhat
  • 3,772
  • 2
  • 26
  • 53
  • What exactly your problem? Creating content provider, or getting (and caching) data from webserver in JSON format? – Konstantin Pribluda Jan 30 '12 at 06:26
  • sorry for such a irresponsible question. I want to know how i can call the web server from a content provider? Is it necessary to create an AsyncTask? – Sudarshan Bhat Jan 30 '12 at 06:31
  • 1
    Maybe this question http://stackoverflow.com/questions/4264589/search-suggestions-from-network-resource-into-quick-search-box can help you. – Eleni Mar 23 '12 at 14:14

1 Answers1

1

ContentProviders are not meant to be asyncronous sources of data. In such a situation, you'll need to create a SyncAdapter which keeps a local database in sync with a remote datastore and expose data from this local database. ContentResolvers integrate well with SyncAdapters to provide for this.

Read point 4 from 'Before you start building' on http://developer.android.com/guide/topics/providers/content-provider-creating.html#DataStorage

Vikram Bodicherla
  • 7,133
  • 4
  • 28
  • 34
  • my web server returns different data depending on the query i send to it. what i mean to say is that there's no fixed repository. And web server also requires authentication. one needs to be logged in to get results. is it possible in this case to get results in android's quick search box widget? – Sudarshan Bhat Jan 30 '12 at 07:11
  • You'll have to do the auth before hand, all the more if you are using the quick search box. You can't bother the user at that point wasting time with authentication. As for "different data based on the query part", can't you abstract it out at some level so as to expose a single datastore interface? – Vikram Bodicherla Jan 30 '12 at 07:26
  • it is like searching for some data on a cloud. i send a query and server will return me records that match the query. how can i use syncadapter in this case? – Sudarshan Bhat Jan 30 '12 at 07:37
  • and with every query i have to send a cookie which is stored in my SharedPreferences. i don't seem to find a way to access Shared Preferences from a content provider – Sudarshan Bhat Jan 30 '12 at 07:43
  • well, out of curiosity, i tried to make a connection to my web server, fetch the response and tried to display it. and it works. all inside the query() function of content provider. – Sudarshan Bhat Jan 30 '12 at 13:22
  • I would be wary of that because of the possible network latency – Vikram Bodicherla Jan 30 '12 at 13:47