java 如何将 Android 应用程序连接到 API 并在列表中显示数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12518364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to connect an Android app to an API and display data in a list?
提问by SurDoc83
I am trying to make an app that connects to an API (via URL) to retrieve sessions of past workshops. The API returns all data in JSON format. I've tried to do a couple of tutorials (using twitter API's) but they are not working. Here is a link to the tutorial: http://www.vogella.com/articles/AndroidJSON/article.html
我正在尝试制作一个连接到 API(通过 URL)的应用程序,以检索过去研讨会的会话。API 以 JSON 格式返回所有数据。我试过做几个教程(使用 twitter API),但它们不起作用。这是教程的链接:http: //www.vogella.com/articles/AndroidJSON/article.html
Any help?
有什么帮助吗?
采纳答案by Kumar Vivek Mitra
Try the Marakana tutorials. It's a set of complete video tutorials on Youtube. JSON parsing has been explained with a Twitter timeline example. I have forgotten the video number of that specific tutorial, but with a little fiddling you will surely find it.
试试马拉卡纳教程。这是一组完整的 Youtube 视频教程。JSON 解析已通过 Twitter 时间线示例进行了解释。我忘记了那个特定教程的视频编号,但稍微摆弄一下,你肯定会找到它。
Link to first tutorial:
第一个教程链接:
回答by THANN Phearum
I would like to recommence to use Retrofit. Less code and get better performance. http://square.github.io/retrofit/
我想重新开始使用改造。更少的代码并获得更好的性能。 http://square.github.io/retrofit/
You can find many code examples and support.
您可以找到许多代码示例和支持。
You can just define your API in Interface
你可以在 Interface 中定义你的 API
public interface GitHubService {
@GET("users/{user}/repos")
Call<List<Repo>> listRepos(@Path("user") String user);
}
And set the main URL of your API
并设置您的 API 的主 URL
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.github.com/")
.build();
GitHubService service = retrofit.create(GitHubService.class);
Final step, Retrofit will convert the response Json to correct Collection
最后一步,Retrofit 会将响应 Json 转换为正确的 Collection
Call<List<Repo>> repos = service.listRepos("octocat");