0

I am having trouble figuring out how to get the object "legacy" if it exists from "https://api.mojang.com/users/profiles/minecraft/" + name. On some "names" the legacy isn't present, so how could I see if it exists aswell and then output that a message and if it is there and the value is "true" then to output another message. Here is my code so far. I am using a gui builder thing,

package net.parkourworld.Poket;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import net.parkourworld.Poket.JsonParserFromUrl.DataObject;
import net.parkourworld.Poket.JsonParserFromUrl.Item;

import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class Window {

protected Shell shell;
public Text name;

/**
 * Launch the application.
 * @param args
 */
public static void main(String[] args) {
    try {
        Window window = new Window();
        window.open();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/**
 * Open the window.
 */
public void open() {
    Display display = Display.getDefault();
    createContents();
    shell.open();
    shell.layout();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
}

/**
 * Create contents of the window.
 */
protected void createContents() {
    shell = new Shell();
    shell.setSize(450, 300);
    shell.setText("SWT Application");

    name = new Text(shell, SWT.BORDER);
    name.setBounds(20, 72, 143, 21);

    Label lblUsername = new Label(shell, SWT.NONE);
    lblUsername.setBounds(20, 51, 55, 15);
    lblUsername.setText("Username");

    Button btnCheck = new Button(shell, SWT.NONE);
    btnCheck.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {              
            public void parseJson(String url){
                try{
                    Reader reader = new InputStreamReader(new URL("https://api.mojang.com/users/profiles/minecraft/" + name).openStream()); //Read the json output
                    Gson gson = new GsonBuilder().create();
                    DataObject obj = gson.fromJson(reader, DataObject.class);
                    System.out.println(obj);
                }catch(Exception e){
                    System.out.println(e);
                }
            }

            private class DataObject{ //This class should match your json object structure
                private int status;
                private String error_message;
                private List<Item> item; // This is for the inner array
                @Override
                public String toString() {
                    return status + " - " + error_message+ " (" + item + ")";
                }
            }

            private class Item{ //This is the inner array class
                public int ID;
                public String Name;
                @Override
                public String toString() {
                    return ID + " - " + Name +"\n";
                }
            }
        }
    });
    btnCheck.setBounds(20, 112, 75, 25);
    btnCheck.setText("Check");

 }
}
YoungHobbit
  • 13,254
  • 9
  • 50
  • 73
Instinct
  • 9
  • 2

1 Answers1

-2

If you have the Json schema or at least the full JSON output use: http://www.jsonschema2pojo.org/ to generate your DataObject.

indranil32
  • 140
  • 1
  • 9
  • I need to get it from a link which is https://api.mojang.com/users/profiles/minecraft/(username) and the username will change depending on what i set it to in the gui builder I am using – Instinct Jan 04 '16 at 06:29
  • It doesn't matter what the URL for getting the JSON Object is!! If you know all the JSON Elements of the output you can generate the POJO from the said link and use it. If you want Javascript style dynamic JSON handling and do not have a issue introducing a little bit of groovy check this : - http://docs.groovy-lang.org/latest/html/gapi/groovy/json/JsonSlurper.html – indranil32 Jan 04 '16 at 07:43
  • I want to use GSON, and also I don't know the output because it changes depending on what "username" is put in the api. – Instinct Jan 04 '16 at 16:02
  • Check this one:- http://stackoverflow.com/questions/12870332/how-can-i-iterate-jsonobject-to-get-individual-items – indranil32 Jan 05 '16 at 09:23