简体   繁体   中英

Search and return result from online database API (Android Studio SDK)

I have spent the last few weeks working on my first Android Application, after studying Java in school. I have managed to make a functional interface with access (limited, but we're getting there!!) to a database. I am now hoping to integrate an online API into my App, but I'm really not sure where to start. I appreciate any help you can give, even if you can simply point me in the right direction for research.

I am using the USDA National Nutrient Database API ( http://ndb.nal.usda.gov/ndb/foods ) and in my App I have got a TextEdit and "Search" Button which should allow the user to search the database for results. I then want these results to populate a Spinner with a choice of all results that contain the searched text. From here the user should be able to specify an AMOUNT of the food type, and see the nutritional information for eg. 100g, 1 item, 5lb etc. Finally, I want to import the Nutritional Data from the Online Database and place them in a DataBase. I have got a API KEY.

Now I feel like I should be able to do much of this on my own, but what I need help with is actually accessing and recovering the data from the site. Unfortunately I have not used an API before, and I don't know where to begin. I would really appreciate any help you can give me in this, even if you could point me in the right direction for resources and tutorials.


I am including my xml code so far for reference . I have chosen not to include my Java for this Activity, as it lacks most functionality as I do not know how to query the database through the API. If you think it will be helpful I will append it, but it's mostly just initialisation statements, getting the values from the text boxes, and initialising buttons with no onClick methods defined yet.

This search box allows the user to type keywords which will then be searched in the database

<EditText
        android:id="@+id/food_search"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/food_search_hint" />

This Search Button will send the request to query the database for the String from the food_search EditText

<Button
        android:id="@+id/food_button_search"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/food_button_search" />

This Spinner will be populated with the results from the database. The line in italics is a placeholder inserted to test that the Spinner is functional.

<Spinner
    android:id="@+id/food_spinner"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    *android:entries="@array/test_food_spinner"*
    android:background="@android:drawable/btn_dropdown"
    android:spinnerMode="dropdown" />

A second, numerical EditText to add the amount/weight of food

<EditText
        android:id="@+id/food_amount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="2"
        android:hint="@string/food_number_hint"
        android:inputType="number"/>

This Spinner holds values such as "g", "kg", "lb", "units". I will create a method to multiply the results from the database by the values specified in the food_amount textEdit and this spinner

<Spinner
        android:id="@+id/food_amount_spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:entries="@array/test_food_amount_spinner"
        android:background="@android:drawable/btn_dropdown"
        android:spinnerMode="dropdown" />

And finally a submit button to add the name and nutritional information from the online database to a local database

<Button
        android:id="@+id/food_submit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/food_button_submit"
        />

Again thank you so much, I really appreciate any help you can give me on this. I am an eager student but I simply don't know where to turn for information on this subject. <3

This is your documentation: http://ndb.nal.usda.gov/ndb/doc/apilist/API-LIST.md Have a look at how you should build new customized requests.

This is sample request: http://api.nal.usda.gov/ndb/list?format=json&lt=g&sort=n&api_key=xoNloOitF8uXEhuREu11T7y64Lz1tntsZGHcZwPs&location=Denver+CO

It returns you a JSON string. You can make HTTP-requests with okhttp .

Then you can parse your results and marshall it to objects. You can do it manually with JSONObject class or use nice json libraries like gson or jackson (my personal favourite is gson).

I'll give you a simplified example, and you shall continue on.

  1. Open http://api.nal.usda.gov/ndb/list?format=json&lt=g&sort=n&api_key=xoNloOitF8uXEhuREu11T7y64Lz1tntsZGHcZwPs&location=Denver+CO
  2. Copy contents and paste to http://jsonviewer.stack.hu , have a look at the structure
  3. Use http://www.jsonschema2pojo.org/ to generate your java classes to which you will marshall your json-response
  4. Using gson or jackson convert you response to objects, for example, like this:

--

Gson gson = new Gson();
FoodResponse fr = gson.fromJson(jsonResponseString, FoodResponse.class);
  1. Show your objects in UI (all your spinners etc)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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