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?
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: