Traveller Store CotI Features New Posts Mark Forums Read Register


Go Back TravellerRPG.com > Citizens of the Imperium > General Traveller Discussions > Software Solutions

Software Solutions Discussions on Traveller related software.

Reply
 
Thread Tools Display Modes
  #1  
Old October 12th, 2017, 10:40 AM
robject's Avatar
robject robject is offline
Marquis
 
Join Date: Mar 2002
Posts: 13,092
Gallery : 9
Visit robject's Blog
robject has disabled reputation
Default Java reading JSON from TravellerMap

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 );
    }
}
__________________
Imperiallines magazine
My Helpful Stuff for Traveller5
IMTU tc+ t5++ 3i(+) au ls+ / OTU 44% an+ dt+ ge- j- jf+ n- pi+ pp+ tr+ tv- uwp+ xb+
Tools Link

Last edited by robject; October 12th, 2017 at 02:33 PM..
Reply With Quote

Welcome!
To see more of this thread, please login or register.
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
My world-builder JSON string robject Software Solutions 1 September 16th, 2016 12:59 PM
My world-builder JSON string robject Imperial Interstellar Scout Service 0 September 16th, 2016 12:45 PM
JSON Sector Data Service robject Software Solutions 26 December 17th, 2014 09:04 PM
Has anyone seen/tried a java implimentation of FFW? Commander Truestar Software Solutions 1 December 19th, 2008 09:32 PM
Java Chargen kafka47 The Lone Star 14 August 2nd, 2005 06:21 PM

This website and its contents are copyright ©2010-2013 Far Future Enterprises. All rights reserved. Traveller is a registered trademark of Far Future Enterprises .
Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright (c) 2010-2013, Far Future Enterprises. All Rights Reserved.