• Welcome to the new COTI server. We've moved the Citizens to a new server. Please let us know in the COTI Website issue forum if you find any problems.
  • We, the systems administration staff, apologize for this unexpected outage of the boards. We have resolved the root cause of the problem and there should be no further disruptions.

Java reading JSON from TravellerMap

robject

SOC-14 10K
Admin Award
Marquis
So I suppose I'll need to actually go get one of the zillion JSON packages for Java. I suppose I can't sneakily use the Rhino interpreter to handily create a map.

Your opinions?

Code:
import java.io.*;
import java.net.*;

public class TravellerMap
{
    public String getWorldByHex( String sector, String hex )
    {
        return get( "https://travellermap.com/data/" + sector + "/" + hex );
    }

    public String getJumpMapByHex( String sector, String hex, int jumpnum )
    {
        return get( "https://travellermap.com/data/" + sector + "/" + hex + "/jump/" + jumpnum );
    }

    private String get( String urlString )
    {
        HttpURLConnection conn = connect( urlString );
        checkConnectionIntegrity( conn );
        String output = readURLResponse( conn );
        if ( conn != null )
            conn.disconnect();
        return output;
    }

    private HttpURLConnection connect( String urlString )
    {
        try
        {
            URL url = new URL(urlString);
            HttpURLConnection conn;
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Accept", "application/json");
            return conn;
        }
        catch( IOException ioe )
        {
            ioe.printStackTrace();
        }
        return null;
    }

    private void checkConnectionIntegrity( HttpURLConnection conn )
    {
        try
        {
            if (conn.getResponseCode() != 200)
            {
                throw new RuntimeException("Failed: HTTP error code: "
                        + conn.getResponseCode());
            }
        }
        catch( IOException ioe )
        {
            ioe.printStackTrace();
        }
    }

    private String readURLResponse( HttpURLConnection conn )
    {
        String output = "";
        try
        {
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;

            while ((line = br.readLine()) != null)
            {
                output += line;
            }
        }
        catch( IOException ioe )
        {
            ioe.printStackTrace();
        }
        return output;
    }

    public static void main( String[] args ) {

        TravellerMap map = new TravellerMap();

        String out = map.getJumpMapByHex( "spin", "1910", 4 );

        System.out.println( out );
    }
}
 
Last edited:
Crawling through the Rhino Heap is a real pain, just go and get GSon or Jackson. It's mostly painless.

Mind, Jackson and GSon "out of the box" are designed to map Java classes to and from JSON. But they readily supply just raw "JsonObjects" or whatever it is so you can "walk the DOM" if you want.

Using Gson:

JsonObject jsonDom = new JsonParser().parse(jsonString).getAsJsonObject();

It's a shame that TM provides Json for the individual hexes, but not for full sectors/subsectors.

The JSON coming back doesn't trivially map to a java class either (I tried), since the names are all CamelCase instead of camelCase. So, those would have to be mapped if you want to get things to a class.
 
There may be sites on the web that will convert JSON to a Java class. I know there are for C# as that's how I am getting the data in my software - I ran the API manually, posted the results a converter, then copied the instant class into my project. Sadly not all his APIs return JSON - the world listing is SEC but as that is a fixed width, that's easy enough to parse as well (reminds me of my old COBOL days...just add in some implied decimal fields and I would get all nostalgic :) )

Having never messed with Java I don't know if that exists.
 
Back
Top