Example usage for org.json JSONObject getInt

List of usage examples for org.json JSONObject getInt

Introduction

In this page you can find the example usage for org.json JSONObject getInt.

Prototype

public int getInt(String key) throws JSONException 

Source Link

Document

Get the int value associated with a key.

Usage

From source file:com.github.koraktor.steamcondenser.community.SteamId.java

/**
 * Resolves a vanity URL of a Steam Community profile to a 64bit numeric
 * SteamID/*from w w w.j ava2  s .  c o  m*/
 *
 * @param vanityUrl The vanity URL of a Steam Community profile
 * @return The 64bit SteamID for the given vanity URL
 * @throws WebApiException if the request to Steam's Web API fails
 */
public static Long resolveVanityUrl(String vanityUrl) throws WebApiException {
    try {
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("vanityurl", vanityUrl);

        String json = WebApi.getJSON("ISteamUser", "ResolveVanityURL", 1, params);
        JSONObject result = new JSONObject(json).getJSONObject("response");

        if (result.getInt("success") != 1) {
            return null;
        }

        // org.json.JSONObject#getLong() seems to be broken
        return Long.parseLong(result.getString("steamid"));
    } catch (JSONException e) {
        throw new WebApiException("Could not parse JSON data.", e);
    }
}

From source file:org.jenkinsci.plugins.testrail.TestRailClient.java

public Project[] getProjects() throws IOException, ElementNotFoundException {
    String body = httpGet("/index.php?/api/v2/get_projects").getBody();
    JSONArray json = new JSONArray(body);
    Project[] projects = new Project[json.length()];
    for (int i = 0; i < json.length(); i++) {
        JSONObject o = json.getJSONObject(i);
        Project p = new Project();
        p.setName(o.getString("name"));
        p.setId(o.getInt("id"));
        projects[i] = p;//  w ww .java 2s . c  o m
    }
    return projects;
}

From source file:org.jenkinsci.plugins.testrail.TestRailClient.java

public Suite[] getSuites(int projectId) throws IOException, ElementNotFoundException {
    String body = httpGet("/index.php?/api/v2/get_suites/" + projectId).getBody();

    JSONArray json;//ww  w  .  j  ava2  s. c o  m
    try {
        json = new JSONArray(body);
    } catch (JSONException e) {
        return new Suite[0];
    }

    Suite[] suites = new Suite[json.length()];
    for (int i = 0; i < json.length(); i++) {
        JSONObject o = json.getJSONObject(i);
        Suite s = new Suite();
        s.setName(o.getString("name"));
        s.setId(o.getInt("id"));
        suites[i] = s;
    }

    return suites;
}

From source file:org.jenkinsci.plugins.testrail.TestRailClient.java

private Section createSectionFromJSON(JSONObject o) {
    Section s = new Section();

    s.setName(o.getString("name"));
    s.setId(o.getInt("id"));

    if (!o.isNull("parent_id")) {
        s.setParentId(String.valueOf(o.getInt("parent_id")));
    } else {/*from w  w w .j  av a 2  s  .  c om*/
        s.setParentId("null");
    }

    s.setSuiteId(o.getInt("suite_id"));

    return s;
}

From source file:org.jenkinsci.plugins.testrail.TestRailClient.java

private Case createCaseFromJson(JSONObject o) {
    Case s = new Case();

    s.setTitle(o.getString("title"));
    s.setId(o.getInt("id"));
    s.setSectionId(o.getInt("section_id"));
    s.setRefs(o.optString("refs"));

    return s;//ww  w.j a va 2  s  . c o  m
}

From source file:org.jenkinsci.plugins.testrail.TestRailClient.java

public Milestone[] getMilestones(int projectId) throws IOException, ElementNotFoundException {
    String body = httpGet("index.php?/api/v2/get_milestones/" + projectId).getBody();
    JSONArray json;/*w  ww .j  a va 2 s  . c om*/
    try {
        json = new JSONArray(body);
    } catch (JSONException e) {
        return new Milestone[0];
    }
    Milestone[] suites = new Milestone[json.length()];
    for (int i = 0; i < json.length(); i++) {
        JSONObject o = json.getJSONObject(i);
        Milestone s = new Milestone();
        s.setName(o.getString("name"));
        s.setId(String.valueOf(o.getInt("id")));
        suites[i] = s;
    }
    return suites;
}

From source file:info.aamulumi.tomate.APIConnection.java

/**
 * Send a GET request and create a list with returned JSON datas.
 * The JSONObject returned by server must have :
 * - success : int - 1 if request is successful
 * - data : JSONArray - contains datas which will be parsed
 *
 * @param url            - url to access
 * @param parseMethod    - name of the method used to parse an object in data
 * @param urlParameters  - parameters send in URL
 * @param bodyParameters - parameters send in body (encoded)
 * @return the list of parsed elements//from   w  ww.j a v a2s .c  o  m
 */
private static <T> ArrayList<T> getItems(String url, String parseMethod, HashMap<String, String> urlParameters,
        HashMap<String, String> bodyParameters) {
    ArrayList<T> list = new ArrayList<>();

    // Get parseMethod
    Class<?>[] cArg = new Class[1];
    cArg[0] = JSONObject.class;

    Method parse;
    try {
        parse = APIConnection.class.getMethod(parseMethod, cArg);
    } catch (NoSuchMethodException e1) {
        e1.printStackTrace();
        return null;
    }

    // Do the request
    JSONObject json = makeHttpRequest(url, GET, urlParameters, bodyParameters);

    if (json == null)
        return null;
    try {
        int success = json.getInt("success");
        // Parse if successful
        if (success == 1) {
            JSONArray data = json.getJSONArray("data");
            for (int i = 0; i < data.length(); i++) {
                @SuppressWarnings("unchecked")
                T tmp = (T) parse.invoke(APIConnection.class, data.getJSONObject(i));
                list.add(tmp);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

    return list;
}

From source file:info.aamulumi.tomate.APIConnection.java

/**
 * Send a HTTP request and parse the returned element.
 * The JSONObject returned by server must have :
 * - success : int - 1 if request is successful
 * - data : JSONObject - element which will be parsed
 *
 * @param url            - url to access
 * @param method         - HTTP method (GET, PUT, ...)
 * @param parseMethod    - name of the method used to parse an object in data
 * @param urlParameters  - parameters send in URL
 * @param bodyParameters - parameters send in body (encoded)
 * @return the list of parsed elements//from   w w  w .  j  a va2 s.  co m
 */
@SuppressWarnings("unchecked")
private static <T> T doHTTPRequestAndParse(String url, String method, String parseMethod,
        HashMap<String, String> urlParameters, HashMap<String, String> bodyParameters) {
    // Get parseMethod
    Class<?>[] cArg = new Class[1];
    cArg[0] = JSONObject.class;

    Method parse;
    try {
        parse = APIConnection.class.getMethod(parseMethod, cArg);
    } catch (NoSuchMethodException e1) {
        e1.printStackTrace();
        return null;
    }

    // Do the request
    JSONObject json = makeHttpRequest(url, method, urlParameters, bodyParameters);

    if (json == null)
        return null;
    try {
        int success = json.getInt("success");
        // Parse if successful
        if (success == 1) {
            return (T) parse.invoke(APIConnection.class, json.getJSONObject("data"));
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

    return null;
}

From source file:info.aamulumi.tomate.APIConnection.java

/**
 * Send a HTTP request and check the returned element.
 * The JSONObject returned by server must have :
 * - success : int - 1 if request is successful
 *
 * @param url            - url to access
 * @param method         - HTTP method (GET, PUT, ...)
 * @param urlParameters  - parameters send in URL
 * @param bodyParameters - parameters send in body (encoded)
 * @return the list of parsed elements//from w  ww  .  j a va2  s  . c om
 */
public static boolean doHTTPRequest(String url, String method, HashMap<String, String> urlParameters,
        HashMap<String, String> bodyParameters) {
    JSONObject json = makeHttpRequest(url, method, urlParameters, bodyParameters);

    if (json == null)
        return false;
    try {
        int success = json.getInt("success");
        // Parse if successful
        if (success == 1) {
            return true;
        }
    } catch (JSONException e) {
        e.printStackTrace();
        return false;
    }

    return false;
}

From source file:CNCServicesServlet.CNCServices.java

/**
 * Demo Call Buy Card Service//w ww. j a v  a 2 s.c o  m
 */
private String BuyCard(HttpServletRequest request) {
    String txtData = "";
    String txtAgentCode = request.getServletContext().getInitParameter("agentCode");
    String txtAgentKey = request.getServletContext().getInitParameter("agentKey");
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DATE, 7);
    Date date = cal.getTime();
    SimpleDateFormat ft = new SimpleDateFormat("YYYYMMddhhmmssss");
    BuyCardRequest bcr = new BuyCardRequest(txtAgentCode, "VT", ft.format(date).toString(), 10000, 2);
    String bcrString = bcr.toString();
    ResponseRequest rr = new ResponseRequest();
    try {
        txtData = Util.Encrypt(txtAgentKey, bcrString);
        Softpin soft = new Softpin();
        String result = soft.getSoftpinSoap12().buyCard(txtAgentCode, txtData);
        JSONObject jsonObject = new JSONObject(result);
        String msg = jsonObject.isNull("msg") ? "" : jsonObject.getString("msg");
        rr.setMsg(msg);
        String tranid = jsonObject.isNull("tranid") ? "" : jsonObject.getString("tranid");
        rr.setTranid(tranid);
        String listCards = jsonObject.isNull("listCards") ? "" : jsonObject.getString("listCards");
        Integer code = jsonObject.getInt("code");
        rr.setCode(code);
        if (code == 1) {
            String listResult = Util.Decrypt(txtAgentKey, listCards);
            rr.setListCards(listResult);
            System.out.println("success");
        } else {
            rr.setListCards("");
            System.out.println("Error=" + msg);
        }
    } catch (Exception e) {
        rr.setListCards("");
        System.out.println("error=" + e.getMessage());
    }
    return rr.toString();
}