简体   繁体   English

如何使用 API 在 Android 上实现搜索功能?

[英]How can I use an API to implement a search functionality on Android?

I am attempting to create a search functionality in an android application and have been doing research on how I can accomplish this while taking in data from a third party API.我正在尝试在一个 android 应用程序中创建一个搜索功能,并且一直在研究如何在从第三方 API 获取数据的同时实现这一点。 I am still learning much about the android environment, so I could use some guidance on how I can go about completing this task.我还在学习很多关于 android 环境的知识,所以我可以使用一些指导来了解如何完成这项任务。 Say for example, I have an API https://www.example.com/api/json/15504877/search with an API key of 15504877, how would I use this link & key so that it pulls the data from this API in my android app upon a user searching for something like pet breed (for example)?例如,我有一个 API https://www.example.com/api/json/15504877/search的 API 密钥为 15504877,我将如何使用此链接和密钥以便它从该 API 中提取数据当用户搜索诸如宠物品种之类的东西时,我的 android 应用程序(例如)? Any information would help, I have a base activity and would like to complete this within it and also have some code below and I will add more information if necessary.任何信息都会有所帮助,我有一个基本活动,并希望在其中完成此活动,并且下面还有一些代码,如有必要,我将添加更多信息。

MainActivity.kt: MainActivity.kt:

 package com.example.application import android.os.Bundle import com.google.android.material.bottomnavigation.BottomNavigationView import androidx.appcompat.app.AppCompatActivity import androidx.navigation.findNavController import androidx.navigation.ui.AppBarConfiguration import androidx.navigation.ui.setupActionBarWithNavController import androidx.navigation.ui.setupWithNavController import com.example.application.databinding.ActivityMainBinding class MainActivity : AppCompatActivity() { private lateinit var binding: ActivityMainBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root) val navView: BottomNavigationView = binding.navView val navController = findNavController(R.id.nav_host_fragment_activity_main) // Passing each menu ID as a set of Ids because each // menu should be considered as top level destinations. val appBarConfiguration = AppBarConfiguration( setOf( R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications ) ) setupActionBarWithNavController(navController, appBarConfiguration) navView.setupWithNavController(navController) } }

HomeViewModel.kt: HomeViewModel.kt:

 package com.example.application.ui.home import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel class HomeViewModel : ViewModel() { private val _text = MutableLiveData<String>().apply { value = "This is home Fragment" } val text: LiveData<String> = _text }

HomeFragment.kt: HomeFragment.kt:

 package com.example.application.ui.home import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.TextView import androidx.fragment.app.Fragment import androidx.lifecycle.ViewModelProvider import com.example.application.databinding.FragmentHomeBinding class HomeFragment : Fragment() { private var _binding: FragmentHomeBinding? = null // This property is only valid between onCreateView and // onDestroyView. private val binding get() = _binding!! override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View { val homeViewModel = ViewModelProvider(this).get(HomeViewModel::class.java) _binding = FragmentHomeBinding.inflate(inflater, container, false) val root: View = binding.root val textView: TextView = binding.textHome homeViewModel.text.observe(viewLifecycleOwner) { textView.text = it } return root } override fun onDestroyView() { super.onDestroyView() _binding = null } }

AndroidManifest.xml: AndroidManifest.xml:

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.application"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.Application"> <activity android:name=".MainActivity" android:exported="true" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>

For an http request to api, you need to use a third-party library.对于 api 的 http 请求,需要使用第三方库。 For example, OkHttp, or, which is recommended, Retrofit (essentially a wrapper over OkHttp).例如,OkHttp,或者推荐的Retrofit (本质上是 OkHttp 的包装器)。 There is a lot of lessons all over the Internet on how to use this library, for example, here .互联网上有很多关于如何使用这个库的课程,例如, 这里 On my own behalf, I would like to add that if you want to follow the mvvm-architecture, you must take out the logic for obtaining data in a separate object (which usually called a repository), and then you need to call method of the repository inside the ViewModel to put the data in LiveData就我个人而言,我想补充一点,如果你想遵循 mvvm 架构,你必须在一个单独的对象(通常称为存储库)中取出获取数据的逻辑,然后你需要调用方法ViewModel 内的存储库,用于将数据放入 LiveData

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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