如何獲取JSON數據以便在我的android原生應用中解析和使用

[英]How to get JSON data for any website to parse and use in my android native app


My understanding is that as an android developer in order to develop native android apps for existing web applications built using Spring MVC or PHP or Angular JS, I need to access the data in JSON format and use in my android app as per my needs. For this I don't have to know anything about the web frameworks which were used to build the websites.

我的理解是,作為一名android開發人員,為了為使用Spring MVC或PHP或棱角JS構建的現有web應用開發本機android應用程序,我需要以JSON格式訪問數據,並根據需要在我的android應用程序中使用。為此,我不需要知道任何關於用於構建網站的web框架的信息。

  1. Is my understanding correct? If no, please correct my understanding.

    我的理解正確嗎?如果沒有,請糾正我的理解。

  2. If yes, should I just ask the developer (or) find that info someplace (posted for use) to know how to access the json data for their website and use it in my code? (or) is there something else I need to do?

    如果是,我是否應該讓開發人員(或)在某個地方(發布用於使用的)找到這些信息,以了解如何訪問他們網站的json數據並在我的代碼中使用它們?(或)我還有別的事要做嗎?

  3. If the developer has to provide the info for JSON, what should I specifically ask for? and What will the developer provide me?

    如果開發人員必須提供JSON信息,我應該特別要求什么?開發人員會給我提供什么?

Thanks for your help in advance!

提前感謝您的幫助!

1 个解决方案

#1


2  

Let me answer your questions, even though they're a bit vague.

讓我回答你的問題,盡管它們有點模糊。

Is my understanding correct?

我的理解正確嗎?

Yes, it is, you can make Http calls to the web API and get and parse the JSON response according to your needs.

是的,您可以對web API進行Http調用,並根據需要獲取和解析JSON響應。

If yes, should I just ask the developer (or) find that info someplace (posted for use) to know how to access the json data for their website and use it in my code? (or) is there something else I need to do?

如果是,我是否應該讓開發人員(或)在某個地方(發布用於使用的)找到這些信息,以了解如何訪問他們網站的json數據並在我的代碼中使用它們?(或)我還有別的事要做嗎?

Of course, if you know the developer, you should ask him. Most of the popular websites who have open API's have some kind of documentation about their JSON responses. A good example is taking a look at reddit's API, you can get any page as a JSON response by simply attaching /.json at the end of any url. This might prove useful if you want to practice parsing json and sending http requests. I'd recommend using OkHttp library for this.

當然,如果你認識開發人員,你應該問問他。大多數有開放API的流行網站都有關於JSON響應的文檔。一個很好的例子是查看reddit的API,您可以通過簡單的附加/獲得任何頁面作為JSON響應。json位於任何url的末尾。如果您希望對json進行解析並發送http請求,那么這可能會非常有用。為此,我建議使用OkHttp庫。

If the developer has to provide the info for JSON, what should I specifically ask for? and What will the developer provide me?

如果開發人員必須提供JSON信息,我應該特別要求什么?開發人員會給我提供什么?

This question I find the most difficult one to answer since it's a bit vague. You ask what you need and what is the request you need to send to get it.

這個問題我覺得最難回答,因為它有點含糊。你問你需要什么,你需要發送什么請求來獲取它。

What this means is what parameters your request should have (the best example being if the site uses authentication, does it use oauth or something else, do you need to first log in and get an access_token as is usually a case). This can vary tremendously depending on what you're trying to get, some requests are POST, some are GET and might not even require any additional parameters etc.

這意味着您的請求應該具有哪些參數(最好的例子是,如果站點使用了身份驗證、是否使用了oauth或其他東西,是否需要首先登錄並獲得access_token,通常是這樣)。這取決於你想要得到什么,有些請求是POST,有些是get,甚至可能不需要任何其他參數等等。

For example if you want to get a list of, for example, all cars from a website, you'd ask or search for a route that returns a JSONArray of cars and you'd presumably wanna send a GET request to that route (with access_token parameter if they use oauth, just an example) and you'd receive a respone, which may look something like this:

例如,如果你想要得到的列表,例如,所有的汽車從一個網站,你會問或搜索的路線返回JSONArray汽車和你大概想要發送一個get請求,路線(access_token參數如果他們使用oauth,只是舉個例子),你會收到一個響應,這可能看起來像這樣:

{ 
   [ {"id" : "1",
    "manufacturer" : "bmw",
    "model" : "320d"},
    {"id" : "1",
    "manufacturer" : "audi",
    "model" : "A6"}
   ]
}

This is purely for demonstrative purposes. The JSON may be arbitrarily more or less complicated, and you can parse it according to your needs, you might wanna use just some of the data contained there. There are a lot of questions on the topic of json parsing, so I won't get into that any further.

這純粹是為了演示目的。JSON可能或多或少是任意復雜的,您可以根據需要對其進行解析,您可能只想使用其中包含的一些數據。關於json解析的主題有很多問題,所以我就不再深入了。

I hope this was informative to you, and if you're looking to make your own API, there are a lot of ways you can do this, you can also find many tutorials on this topic. I've done this in Laravel and Symfony PHP frameworks, and let me tell you, it's not that hard at all. All you need to do is always return a JSON and that's basically it.

我希望這對您來說是有用的,如果您想要創建自己的API,有很多方法可以做到這一點,您也可以在這個主題上找到很多教程。我在Laravel和Symfony PHP框架中做過這個,讓我告訴你,這並不難。你只需要返回一個JSON就行了。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/06/29/725c7dc5dbcc54ded3f4d010b229de90.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com