我正在制作一个 java 程序,我正在寻找一种方法来检查是否有可用的新版本我在 parse.com 上创建了一个帐户。
API:CLICK
我完全在 java 上工作,我有点新,所以保持简单。
我已经创建了一个带有版本号的类,如果现在的版本是 beta,我需要一种方法将这 2 个值转换为 int 和 boolean 并进行其余的处理。
我如何从 parse.com 类中获取值?
最佳答案
以最简单的方式,您可以使用 json.org 提供的库对于 java(下载 here)并使用类似于以下的代码:
URL url = new URL("http://my.domain.com/data.json");
JSONTokener tokener = new JSONTokener(url.openStream());
JSONObject root = new JSONObject(tokener);
当然,您可以使用其他一些提供更多灵 active 的库,例如 Gson或 Jackson
可以找到 gson 和 jackson 的小样本 here和 here分别。
对于基本身份验证,您可以使用类似的东西:
Authenticator.setDefault(new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { 返回新的 PasswordAuthentication ("usr", "pass".toCharArray()); } });
相关的东西here和 documentation .
此外,HTTPClient 是一个很棒的 HTTP 相关库,请检查它 here如果你愿意的话。
如果您使用的是 Maven,则可以为 json.org 添加以下依赖项:
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
Gson :
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.2</version>
</dependency>
Jackson(主要版本 1,但 2 已经可用,请在网站上查看):
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.12</version>
</dependency>
对于 HTTP 客户端版本 4:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2.4</version>
</dependency>
关于java - 从远程网站读取 JSON 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16244171/