|
| 1 | +//package com.SifterReader; |
| 2 | + |
| 3 | +/* |
| 4 | + * SifterReader.java |
| 5 | + * |
| 6 | + * Copyright 2012 Mark Mikofski <marko@linuxBox> |
| 7 | + * |
| 8 | + * This program is free software; you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU General Public License as published by |
| 10 | + * the Free Software Foundation; either version 2 of the License, or |
| 11 | + * (at your option) any later version. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * GNU General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU General Public License |
| 19 | + * along with this program; if not, write to the Free Software |
| 20 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 21 | + * MA 02110-1301, USA. |
| 22 | + * |
| 23 | + * |
| 24 | + */ |
| 25 | + |
| 26 | +import java.net.*; |
| 27 | +import java.io.*; |
| 28 | +import org.json.*; |
| 29 | + |
| 30 | +/** SifterAPI Reader using org.json library **/ |
| 31 | + |
| 32 | +public class SifterReader { |
| 33 | + |
| 34 | + public static void main (String[] args) throws Exception { |
| 35 | + /** Enter your sifter account and access key in the |
| 36 | + ** string fields below */ |
| 37 | + |
| 38 | + // create URL object to SifterAPI |
| 39 | + String yourAccount = "your-account-name"; // enter your account name |
| 40 | + String yourDomain = "yourAccount" + ".sifterapp.com"; |
| 41 | + URL sifter = new URL("https",yourDomain,"/api/projects"); |
| 42 | + |
| 43 | + // open connectino to SifterAPI |
| 44 | + URLConnection sifterConnection = sifter.openConnection(); |
| 45 | + |
| 46 | + // add Access Key to header request |
| 47 | + String yourAccessKey = "your-sifterapi-32char-access-key"; // enter your access key |
| 48 | + sifterConnection.setRequestProperty("X-Sifter-Token", |
| 49 | + yourAccessKey); |
| 50 | + |
| 51 | + // add Accept: application/json to header - also not necessary |
| 52 | + sifterConnection.addRequestProperty("Accept","application/json"); |
| 53 | + |
| 54 | + // URLconnection.connect() not necessary |
| 55 | + // getInputStream will connect |
| 56 | + |
| 57 | + // don't make a content handler, org.json reads a stream! |
| 58 | + |
| 59 | + // create buffer and open input stream |
| 60 | + BufferedReader in = new BufferedReader( |
| 61 | + new InputStreamReader( |
| 62 | + sifterConnection.getInputStream())); |
| 63 | + // don't readline, create stringbuilder, append, create string |
| 64 | + |
| 65 | + // construct json tokener from input stream or buffered reader |
| 66 | + JSONTokener x = new JSONTokener(in); |
| 67 | + |
| 68 | + // initialize "projects" JSONObject from string |
| 69 | + JSONObject projects = new JSONObject(x); |
| 70 | + |
| 71 | + // prettyprint "projects" |
| 72 | + System.out.println("************ projects ************"); |
| 73 | + System.out.println(projects.toString(2)); |
| 74 | + |
| 75 | + // array of projects |
| 76 | + JSONArray array = projects.getJSONArray("projects"); |
| 77 | + int arrayLength = array.length(); |
| 78 | + JSONObject[] p = new JSONObject[arrayLength]; |
| 79 | + |
| 80 | + // projects |
| 81 | + for (int i=0;i<arrayLength;i++) { |
| 82 | + p[i] = array.getJSONObject(i); |
| 83 | + System.out.println("************ project: " + (i+1) + " ************"); |
| 84 | + System.out.println(p[i].toString(2)); |
| 85 | + } |
| 86 | + |
| 87 | + // check field names |
| 88 | + String[] checkNames = { |
| 89 | + "api_url", |
| 90 | + "archived", |
| 91 | + "api_issues_url", |
| 92 | + "milestones_url", |
| 93 | + "api_milestones_url", |
| 94 | + "api_categories_url", |
| 95 | + "issues_url", |
| 96 | + "name", |
| 97 | + "url", |
| 98 | + "api_people_url", |
| 99 | + "primary_company_name"}; |
| 100 | + |
| 101 | + // project field names |
| 102 | + String[] fieldNames = JSONObject.getNames(p[0]); |
| 103 | + int numKeys = p[0].length(); |
| 104 | + SifterProj[] proj = new SifterProj[arrayLength]; |
| 105 | + |
| 106 | + for (int j=0;j<arrayLength;j++) { |
| 107 | + proj[j] = new SifterProj(p[j].get("api_url").toString(), |
| 108 | + p[j].get("archived").toString(), |
| 109 | + p[j].get("api_issues_url").toString(), |
| 110 | + p[j].get("milestones_url").toString(), |
| 111 | + p[j].get("api_milestones_url").toString(), |
| 112 | + p[j].get("api_categories_url").toString(), |
| 113 | + p[j].get("issues_url").toString(), |
| 114 | + p[j].get("name").toString(), |
| 115 | + p[j].get("url").toString(), |
| 116 | + p[j].get("api_people_url").toString(), |
| 117 | + p[j].get("primary_company_name").toString()); |
| 118 | + System.out.println("************ project: " + (j+1) + " ************"); |
| 119 | + for (int i=0;i<numKeys;i++) { |
| 120 | + System.out.print(fieldNames[i]); |
| 121 | + System.out.print(" : "); |
| 122 | + System.out.println(p[j].get(fieldNames[i])); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments