【问题标题】:Search and return result from online database API (Android Studio SDK)从在线数据库API(Android Studio SDK)搜索并返回结果
【发布时间】:2015-09-09 19:54:10
【问题描述】:

在学校学习 Java 之后,过去几周我一直在开发我的第一个 Android 应用程序。我已经设法制作了一个可以访问数据库的功能界面(有限,但我们已经到了那里!!)。我现在希望将在线 API 集成到我的应用程序中,但我真的不知道从哪里开始。感谢您提供的任何帮助,即使您可以简单地为我指明正确的研究方向。

我正在使用美国农业部国家营养数据库 API (http://ndb.nal.usda.gov/ndb/foods),在我的应用程序中,我有一个 TextEdit 和“搜索”按钮,应该允许用户在数据库中搜索结果。然后,我希望这些结果可以选择包含搜索文本的所有结果来填充 Spinner。从这里用户应该能够指定食物类型的数量,并查看例如营养信息。 100 克、1 件、5 磅等。最后,我想从在线数据库中导入营养数据并将它们放入数据库中。我有一个 API KEY。

现在我觉得我应该能够自己完成大部分工作,但我需要帮助的是实际访问和恢复站点中的数据。不幸的是,我以前没有使用过 API,也不知道从哪里开始。我非常感谢您在这方面给我的任何帮助,即使您可以为我指明正确的资源和教程方向。


到目前为止,我已经包含了我的 xml 代码供参考。 我选择不为此 Activity 包含我的 Java,因为它缺少大部分功能,因为我不知道如何通过 API 查询数据库。如果您认为它会有所帮助,我会附加它,但它主要是初始化语句,从文本框中获取值,以及尚未定义 onClick 方法的初始化按钮。

此搜索框允许用户输入关键字,然后在数据库中搜索这些关键字

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

此搜索按钮将发送请求以从 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" />

此 Spinner 将使用来自数据库的结果进行填充。斜体线是插入的占位符,用于测试 Spinner 是否正常工作。

<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" />

第二个数字 EditText 用于添加食物的数量/重量

<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"/>

此 Spinner 包含诸如“g”、“kg”、“lb”、“units”之类的值。我将创建一个方法,将数据库中的结果乘以 food_amount textEdit 和这个微调器中指定的值

<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" />

最后是一个提交按钮,用于将在线数据库中的名称和营养信息添加到本地数据库

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

再次非常感谢您,非常感谢您在这方面给我的任何帮助。我是一个热心的学生,但我根本不知道在哪里可以找到关于这个主题的信息。

【问题讨论】:

    标签: java android sql database


    【解决方案1】:

    这是您的文档: http://ndb.nal.usda.gov/ndb/doc/apilist/API-LIST.md 看看您应该如何构建新的自定义请求。

    这是示例请求: http://api.nal.usda.gov/ndb/list?format=json&lt=g&sort=n&api_key=xoNloOitF8uXEhuREu11T7y64Lz1tntsZGHcZwPs&location=Denver+CO

    它会返回一个 JSON 字符串。 您可以使用 okhttp 发出 HTTP 请求。

    然后您可以解析结果并将其编组为对象。 您可以使用 JSONObject 类手动完成,也可以使用漂亮的 json 库,例如 gsonjackson(我个人最喜欢的是 gson)。

    我给你一个简化的例子,你继续。

    1. 打开 http://api.nal.usda.gov/ndb/list?format=json&lt=g&sort=n&api_key=xoNloOitF8uXEhuREu11T7y64Lz1tntsZGHcZwPs&location=Denver+CO
    2. 复制内容粘贴到http://jsonviewer.stack.hu,看看 在结构上
    3. 使用http://www.jsonschema2pojo.org/ 生成您的Java 类以 您将编组您的 json 响应
    4. 使用 gson 或 jackson 将您的响应转换为对象,例如, 像这样:

    --

    Gson gson = new Gson();
    FoodResponse fr = gson.fromJson(jsonResponseString, FoodResponse.class);
    
    1. 在 UI 中显示您的对象(所有微调器等)

    【讨论】:

    • 非常感谢您的建议!!感谢您也为我找到该文档 - 我会根据您的建议来研究 JSON、okhttp 和 gson。我真的很感激,你帮了我很大的忙谢谢:)
    猜你喜欢
    • 2017-11-09
    • 2015-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-26
    • 2013-02-23
    • 1970-01-01
    相关资源
    最近更新 更多