4

I am making a java program and I am looking for a way to check if theres a new version available I made an account on parse.com.
API: CLICK
I am working entirely on java and I am a bit new so keep it simple.
I have created a class with the version number and if the version is beta now i need a way to get these 2 values into a int and a boolean and do the rest of the proccessing. How do i take the values from parse.com class?

2 Answers 2

6

In the simplest way, you could use the library available at json.org for java (download here) and use a code similar to:

URL url = new URL("http://my.domain.com/data.json");
JSONTokener tokener = new JSONTokener(url.openStream());
JSONObject root = new JSONObject(tokener);

But of course you could use some other library that offers a bit more of flexibility like Gson or Jackson

A small sample for gson and jackson can be found here and here respectively.

For basic authentication you could use something like:

Authenticator.setDefault(new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication ("usr", "pass".toCharArray()); } });

Something related here and documentation.

Also, HTTPClient is a great library for HTTP related stuff, please check it here if you are willing to.


If you are using Maven you can add the following dependency for 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 (with major version 1, but 2 is already available, check at the website):

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-asl</artifactId>
    <version>1.9.12</version>
</dependency>

For HTTP Client Version 4:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.2.4</version>
</dependency>
Sign up to request clarification or add additional context in comments.

3 Comments

+1 for gson, +1 for jackson, -1 for json.org. Net +1. Lucky you!
I keep getting an java.io.IOException: Server returned HTTP response code: 401 for URL: api.parse.com/1/classes/NicksNoteVersion/xngGDAa1k3 any ideas?
Yes, this is due the basic authentication you need to provide, check my last comment.
0

You can make the http requests using standard Java API. Check here.

You can parse the response by using gson library. Check here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.