List of usage examples for org.json JSONObject getJSONObject
public JSONObject getJSONObject(String key) throws JSONException
From source file:org.collectionspace.chain.csp.persistence.services.relation.TestRelationsThroughWebapp.java
@Test public void testMultipleCreate() throws Exception { // Create test cataloging ServletTester jetty = tester.setupJetty(); HttpTester out = tester.POSTData("/cataloging/", tester.makeSimpleRequest(tester.getResourceString("obj3.json")), jetty); String id1 = out.getHeader("Location"); out = tester.POSTData("/cataloging/", tester.makeSimpleRequest(tester.getResourceString("obj3.json")), jetty);/*from w ww. j a v a 2 s.c o m*/ String id2 = out.getHeader("Location"); out = tester.POSTData("/cataloging/", tester.makeSimpleRequest(tester.getResourceString("obj3.json")), jetty); String id3 = out.getHeader("Location"); String[] path1 = id1.split("/"); String[] path2 = id2.split("/"); String[] path3 = id3.split("/"); // Do the rleation JSONObject data1 = createRelation(path3[1], path3[2], "affects", path1[1], path1[2], false); JSONObject data2 = createRelation(path3[1], path3[2], "affects", path2[1], path2[2], false); JSONArray datas = new JSONArray(); datas.put(data1); datas.put(data2); JSONObject data = new JSONObject(); data.put("items", datas); out = tester.POSTData("/relationships", data, jetty); // Check it out = tester.GETData(id3, jetty); JSONObject data3 = new JSONObject(out.getContent()); JSONArray rel3 = data3.getJSONObject("relations").getJSONArray("cataloging"); assertNotNull(rel3); assertEquals(2, rel3.length()); tester.DELETEData(id1, jetty); tester.DELETEData(id2, jetty); tester.DELETEData(id3, jetty); tester.stopJetty(jetty); }
From source file:org.collectionspace.chain.csp.persistence.services.relation.TestRelationsThroughWebapp.java
@Test public void testUpdate() throws Exception { // Create test cataloging ServletTester jetty = tester.setupJetty(); HttpTester out = tester.POSTData("/cataloging/", tester.makeSimpleRequest(tester.getResourceString("obj3.json")), jetty); String id1 = out.getHeader("Location"); out = tester.POSTData("/cataloging/", tester.makeSimpleRequest(tester.getResourceString("obj3.json")), jetty);// w w w . j a va 2 s . co m String id2 = out.getHeader("Location"); out = tester.POSTData("/cataloging/", tester.makeSimpleRequest(tester.getResourceString("obj3.json")), jetty); String id3 = out.getHeader("Location"); String[] path1 = id1.split("/"); String[] path2 = id2.split("/"); String[] path3 = id3.split("/"); // Create a relation 3 -> 1 out = tester.POSTData("/relationships", createRelation(path3[1], path3[2], "affects", path1[1], path1[2], true), jetty); // Get csid JSONObject data = new JSONObject(out.getContent()); log.info(out.getContent()); String csid1 = data.getString("csid"); assertNotNull(csid1); // Update it to 2 -> 1 out = tester.PUTData("/relationships/" + csid1, createRelation(path2[1], path2[2], "affects", path1[1], path1[2], true), jetty); log.info(out.getContent()); // Check it out = tester.GETData(id1, jetty); JSONObject data1 = new JSONObject(out.getContent()); JSONObject rel1 = data1.getJSONObject("relations"); out = tester.GETData(id2, jetty); JSONObject data2 = new JSONObject(out.getContent()); log.info(out.getContent()); JSONArray rel2 = data2.getJSONObject("relations").getJSONArray("cataloging"); out = tester.GETData(id3, jetty); JSONObject data3 = new JSONObject(out.getContent()); JSONObject rel3 = data3.getJSONObject("relations"); // clean up after tester.DELETEData("/relationships/" + csid1, jetty); tester.DELETEData(id1, jetty); tester.DELETEData(id2, jetty); tester.DELETEData(id3, jetty); //test assertNotNull(rel1); assertEquals(0, rel1.length()); assertNotNull(rel2); assertEquals(1, rel2.length()); assertNotNull(rel3); assertEquals(0, rel3.length()); tester.stopJetty(jetty); }
From source file:org.collectionspace.chain.csp.persistence.services.relation.TestRelationsThroughWebapp.java
@Test public void testOneWayWorksInUpdate() throws Exception { ServletTester jetty = tester.setupJetty(); HttpTester out = tester.POSTData("/intake/", tester.makeSimpleRequest(tester.getResourceString("2007.4-a.json")), jetty); String id1 = out.getHeader("Location"); out = tester.POSTData("/cataloging/", tester.makeSimpleRequest(tester.getResourceString("obj3.json")), jetty);//from w w w. j a va 2s. c om String id2 = out.getHeader("Location"); out = tester.POSTData("/acquisition/", tester.makeSimpleRequest(tester.getResourceString("2005.017.json")), jetty); String id3 = out.getHeader("Location"); String[] path1 = id1.split("/"); String[] path2 = id2.split("/"); String[] path3 = id3.split("/"); // Create a relation 3 <-> 1 out = tester.POSTData("/relationships", createRelation(path3[1], path3[2], "affects", path1[1], path1[2], false), jetty); // Get csid JSONObject data = new JSONObject(out.getContent()); String csid1 = data.getString("csid"); assertNotNull(csid1); // Update to 2 <-> 1 keeping one-way false out = tester.PUTData("/relationships/" + csid1, createRelation(path2[1], path2[2], "affects", path1[1], path1[2], false), jetty); // Check it out = tester.GETData(id1, jetty); JSONObject data1 = new JSONObject(out.getContent()); JSONArray rel1 = data1.getJSONObject("relations").getJSONArray("cataloging"); assertNotNull(rel1); assertEquals(1, rel1.length()); out = tester.GETData(id2, jetty); JSONObject data2 = new JSONObject(out.getContent()); JSONArray rel2 = data2.getJSONObject("relations").getJSONArray("intake"); assertNotNull(rel2); assertEquals(1, rel2.length()); out = tester.GETData(id3, jetty); JSONObject data3 = new JSONObject(out.getContent()); JSONObject rel3 = data3.getJSONObject("relations"); assertNotNull(rel3); assertEquals(0, rel3.length()); // Update to 1 -> 3, making one-way true //String csid2 = rel1.getJSONObject(0).getString("relid"); out = tester.PUTData("/relationships/" + csid1, createRelation(path1[1], path1[2], "affects", path3[1], path3[2], true), jetty); // Check it out = tester.GETData(id1, jetty); data1 = new JSONObject(out.getContent()); rel1 = data1.getJSONObject("relations").getJSONArray("acquisition"); assertNotNull(rel1); assertEquals(1, rel1.length()); out = tester.GETData(id2, jetty); data2 = new JSONObject(out.getContent()); JSONObject rel2a = data2.getJSONObject("relations"); assertNotNull(rel2a); assertEquals(0, rel2a.length()); out = tester.GETData(id3, jetty); data3 = new JSONObject(out.getContent()); rel3 = data3.getJSONObject("relations"); assertNotNull(rel3); assertEquals(0, rel3.length()); // Update to 3 -> 1, keeping one way true out = tester.PUTData("/relationships/" + csid1, createRelation(path3[1], path3[2], "affects", path1[1], path1[2], true), jetty); // Check it out = tester.GETData(id1, jetty); data1 = new JSONObject(out.getContent()); JSONObject rel1a = data1.getJSONObject("relations"); assertNotNull(rel1a); assertEquals(0, rel1a.length()); out = tester.GETData(id2, jetty); data2 = new JSONObject(out.getContent()); rel2a = data2.getJSONObject("relations"); assertNotNull(rel2a); assertEquals(0, rel2a.length()); out = tester.GETData(id3, jetty); data3 = new JSONObject(out.getContent()); JSONArray rel3a = data3.getJSONObject("relations").getJSONArray("intake"); assertNotNull(rel3a); assertEquals(1, rel3a.length()); // Update to 1 <-> 2, making one way false out = tester.PUTData("/relationships/" + csid1, createRelation(path1[1], path1[2], "affects", path2[1], path2[2], false), jetty); // Check it out = tester.GETData(id1, jetty); data1 = new JSONObject(out.getContent()); rel1 = data1.getJSONObject("relations").getJSONArray("cataloging"); assertNotNull(rel1); assertEquals(1, rel1.length()); out = tester.GETData(id2, jetty); data2 = new JSONObject(out.getContent()); rel2 = data2.getJSONObject("relations").getJSONArray("intake"); assertNotNull(rel2); assertEquals(1, rel2.length()); out = tester.GETData(id3, jetty); data3 = new JSONObject(out.getContent()); rel3 = data3.getJSONObject("relations"); assertNotNull(rel3); assertEquals(0, rel3.length()); // clean up after tester.DELETEData(id1, jetty); tester.DELETEData(id2, jetty); tester.DELETEData(id3, jetty); tester.stopJetty(jetty); }
From source file:org.collectionspace.chain.csp.persistence.services.relation.TestRelationsThroughWebapp.java
@Test public void testRelationshipType() throws Exception { // Create test cataloging ServletTester jetty = tester.setupJetty(); HttpTester out = tester.POSTData("/cataloging/", tester.makeSimpleRequest(tester.getResourceString("obj3.json")), jetty); String id1 = out.getHeader("Location"); out = tester.POSTData("/intake/", tester.makeSimpleRequest(tester.getResourceString("2007.4-a.json")), jetty);/*from ww w.ja va2 s.c o m*/ String id2 = out.getHeader("Location"); String[] path1 = id1.split("/"); String[] path2 = id2.split("/"); // Relate them out = tester.POSTData("/relationships/", createRelation(path2[1], path2[2], "affects", path1[1], path1[2], false), jetty); String csid = new JSONObject(out.getContent()).getString("csid"); // Check types out = tester.GETData(id1, jetty); JSONObject data1 = new JSONObject(out.getContent()); JSONArray rels1 = data1.getJSONObject("relations").getJSONArray("intake"); assertNotNull(rels1); assertEquals(1, rels1.length()); JSONObject rel1 = rels1.getJSONObject(0); assertEquals(rel1.getString("recordtype"), "intake"); out = tester.GETData(id2, jetty); JSONObject data2 = new JSONObject(out.getContent()); JSONArray rels2 = data2.getJSONObject("relations").getJSONArray("cataloging"); assertNotNull(rels2); assertEquals(1, rels2.length()); JSONObject rel2 = rels2.getJSONObject(0); assertEquals(rel2.getString("recordtype"), "cataloging"); // clean up after tester.DELETEData("/relationships/" + csid, jetty); tester.DELETEData(id1, jetty); tester.DELETEData(id2, jetty); tester.stopJetty(jetty); }
From source file:org.collectionspace.chain.csp.persistence.services.relation.TestRelationsThroughWebapp.java
@Test public void testSearchList() throws Exception { // Check list is empty ServletTester jetty = tester.setupJetty(); HttpTester out = tester.GETData("/relationships/", jetty); JSONArray items = new JSONObject(out.getContent()).getJSONArray("items"); Integer offset = items.length(); // assertEquals(0,items.length()); // Create some cataloging out = tester.POSTData("/intake/", tester.makeSimpleRequest(tester.getResourceString("2007.4-a.json")), jetty);//from w ww .j av a2s . com String id1 = out.getHeader("Location"); out = tester.POSTData("/cataloging/", tester.makeSimpleRequest(tester.getResourceString("obj3.json")), jetty); String id2 = out.getHeader("Location"); out = tester.POSTData("/acquisition/", tester.makeSimpleRequest(tester.getResourceString("2005.017.json")), jetty); String id3 = out.getHeader("Location"); String[] path1 = id1.split("/"); String[] path2 = id2.split("/"); String[] path3 = id3.split("/"); // Add a relation rel1: 2 -> 1 out = tester.POSTData("/relationships/", createRelation(path2[1], path2[2], "affects", path1[1], path1[2], true), jetty); String csid1 = new JSONObject(out.getContent()).getString("csid"); out = tester.GETData("/relationships/" + csid1, jetty); JSONObject rel1 = new JSONObject(out.getContent()); assertEquals(path2[2], rel1.getJSONObject("source").getString("csid")); assertEquals(path1[2], rel1.getJSONObject("target").getString("csid")); // Add some more relations: rel2: 2 -> 3 ; rel 3: 3 -> 1 (new type) out = tester.POSTData("/relationships/", createRelation(path2[1], path2[2], "affects", path3[1], path3[2], true), jetty); String csid2 = new JSONObject(out.getContent()).getString("csid"); out = tester.POSTData("/relationships/", createRelation(path3[1], path3[2], "broader", path1[1], path1[2], true), jetty); String csid3 = new JSONObject(out.getContent()).getString("csid"); // Total length should be 3 XXX pagination & offset // out = GETData("/relationships",jetty); // items=new JSONObject(out.getContent()).getJSONArray("items"); // assertEquals(3,items.length()); // Should be two starting at 2 out = tester.GETData("/relationships/search?source=" + path2[1] + "/" + path2[2], jetty); items = new JSONObject(out.getContent()).getJSONArray("items"); assertEquals(2, items.length()); // Should be one staring at 3, none at 1 out = tester.GETData("/relationships/search?source=" + path3[1] + "/" + path3[2], jetty); items = new JSONObject(out.getContent()).getJSONArray("items"); assertEquals(1, items.length()); out = tester.GETData("/relationships/search?source=" + path1[1] + "/" + path1[2], jetty); items = new JSONObject(out.getContent()).getJSONArray("items"); assertEquals(0, items.length()); // Targets: two at 1, none at 2, one at 3 out = tester.GETData("/relationships/search?target=" + path1[1] + "/" + path1[2], jetty); items = new JSONObject(out.getContent()).getJSONArray("items"); assertEquals(2, items.length()); out = tester.GETData("/relationships/search?target=" + path2[1] + "/" + path2[2], jetty); items = new JSONObject(out.getContent()).getJSONArray("items"); assertEquals(0, items.length()); out = tester.GETData("/relationships/search?target=" + path3[1] + "/" + path3[2], jetty); items = new JSONObject(out.getContent()).getJSONArray("items"); assertEquals(1, items.length()); // out=GETData("/relationships/search?type=broader",null); // items=new JSONObject(out.getContent()).getJSONArray("items"); // assertEquals(1,items.length()); // Combination: target = 1, type = affects; just one out = tester.GETData("/relationships/search?type=affects&target=" + path1[1] + "/" + path1[2], jetty); items = new JSONObject(out.getContent()).getJSONArray("items"); assertEquals(1, items.length()); // Combination: source = 2, target = 3; just one out = tester.GETData("/relationships/search?source=" + path2[1] + "/" + path2[2] + "&target=" + path3[1] + "/" + path3[2], jetty); items = new JSONObject(out.getContent()).getJSONArray("items"); assertEquals(1, items.length()); // clean up after tester.DELETEData("/relationships/" + csid1, jetty); tester.DELETEData("/relationships/" + csid2, jetty); tester.DELETEData("/relationships/" + csid3, jetty); tester.DELETEData(id1, jetty); tester.DELETEData(id2, jetty); tester.DELETEData(id3, jetty); tester.stopJetty(jetty); }
From source file:com.facebook.android.MainPage.java
/** Called when the activity is first created. */ @Override/* w w w .ja va2s .co m*/ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (APP_ID == null) { Util.showAlert(this, "Warning", "Facebook Applicaton ID must be " + "specified before running this example: see Example.java"); } setContentView(R.layout.main); mMovieNameInput = (EditText) findViewById(R.id.title); mMediaSpinner = (Spinner) findViewById(R.id.media); mSearchButton = (Button) findViewById(R.id.search); mFacebook = new Facebook(APP_ID); mAsyncRunner = new AsyncFacebookRunner(mFacebook); SessionStore.restore(mFacebook, this); SessionEvents.addAuthListener(new SampleAuthListener()); SessionEvents.addLogoutListener(new SampleLogoutListener()); // set up the Spinner for the media list selection ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.media_list, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); mMediaSpinner.setAdapter(adapter); //for search button final Context context = this; mSearchButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { String servletURL; String movieName = mMovieNameInput.getText().toString(); // check the input text of movie, if the text is empty give user alert movieName = movieName.trim(); if (movieName.length() == 0) { Toast toast = Toast.makeText(context, "Please enter a movie name", Toast.LENGTH_LONG); toast.setGravity(Gravity.TOP | Gravity.CENTER, 0, 70); toast.show(); } // if movie name is not empty else { // remove any extra whitespace movieName = movieName.replaceAll("\\s+", "+"); String mediaList = mMediaSpinner.getSelectedItem().toString(); if (mediaList.equals("Feature Film")) { mediaList = "feature"; } mediaList = mediaList.replaceAll("\\s+", "+"); // construct the query string // construct the final URL to Servlet //String servletString = "?" + "title=" + movieName + "&" + "title_type=" + mediaList; //servletURL = "http://cs-server.usc.edu:10854/examples/servlet/Amovie" //+ servletString; //String servletString = "?" + "title=" + movieName + "&" + "media=" + mediaList; //servletURL = "http://cs-server.usc.edu:34404/examples/servlet/HelloWorldExample?title=" + movieName + "&" + "media=" + mediaList; //+ servletString; servletURL = "http://cs-server.usc.edu:10854/examples/servlet/Amovie?title=" + movieName + "&" + "title_type=" + mediaList; BufferedReader in = null; try { // REFERENCE: this part of code is modified from: // "Example of HTTP GET Request using HttpClient in Android" // http://w3mentor.com/learn/java/android-development/android-http-services/example-of-http-get-request-using-httpclient-in-android/ // get response (JSON string) from Servlet HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(); request.setURI(new URI(servletURL)); HttpResponse response = client.execute(request); in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuffer sb = new StringBuffer(""); String line = ""; String NL = System.getProperty("line.separator"); while ((line = in.readLine()) != null) { sb.append(line + NL); } in.close(); String page = sb.toString(); //test for JSON string /*LinearLayout lView = new LinearLayout(context); TextView myText = new TextView(context); myText.setText(page); lView.addView(myText); setContentView(lView);*/ // convert the JSON string to real JSON and get out the movie JSON array // to check if there is any movie data JSONObject finalJson; JSONObject movieJson; JSONArray movieJsonArray; finalJson = new JSONObject(page); movieJson = finalJson.getJSONObject("results"); //System.out.println(movieJson); movieJsonArray = movieJson.getJSONArray("result"); // if the response contains some movie data if (movieJsonArray.length() != 0) { // start the ListView activity, and pass the JSON string to it Intent intent = new Intent(context, MovieListActivity.class); intent.putExtra("finalJson", page); startActivity(intent); } // if the response does not contain any movie data, // show user that there is no result for this search else { Toast toast = Toast.makeText(getBaseContext(), "No movie found for this search", Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); } } catch (URISyntaxException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } } } }); }
From source file:org.hubiquitus.twitter4j_1_1.stream.HStream.java
public void fireEvent(JSONObject item) { int code = item.has("text") ? 1 : item.has("warning") ? 2 : item.has("delete") ? 3 : item.has("scrub_geo") ? 4 : item.has("limit") ? 5 : item.has("status_withheld") ? 6 : item.has("user_withheld") ? 7 : item.has("disconnect") ? 8 : 0; for (HStreamListner listener : listeners) { switch (code) { case 1:/*from ww w . j a va 2 s . c om*/ listener.onStatus(item); break; case 2: listener.onStallWarning(item); break; case 3: listener.onLocationDeletionNotices(item); break; case 4: listener.onLimitNotices(item); break; case 5: listener.onStatusDeletionNotices(item); break; case 6: listener.onStatusWithheld(item); break; case 7: listener.onUserWithheld(item); break; case 8: listener.onDisconnectMessages(item); break; default: listener.onOtherMessage(item); } } if (code == 8) { try { displayLogMessage(item.getJSONObject("disconnect").getInt("code")); } catch (JSONException e) { log.error("can not fetch the error code for disconnection", e); } stop(); loop = new Loop(this); loop.owner.start(); } }
From source file:org.eclipse.orion.server.tests.servlets.git.GitAddTest.java
@Test public void testAddChanged() throws Exception { URI workspaceLocation = createWorkspace(getMethodName()); JSONObject project = createProjectOrLink(workspaceLocation, getMethodName(), gitDir.toString()); JSONObject testTxt = getChild(project, "test.txt"); modifyFile(testTxt, "hello"); addFile(testTxt);/*from w ww . java 2 s.co m*/ JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT); String gitStatusUri = gitSection.getString(GitConstants.KEY_STATUS); assertStatus(new StatusResult().setChanged(1), gitStatusUri); }
From source file:org.eclipse.orion.server.tests.servlets.git.GitAddTest.java
@Test public void testAddMissing() throws Exception { URI workspaceLocation = createWorkspace(getMethodName()); String projectName = getMethodName(); JSONObject project = createProjectOrLink(workspaceLocation, projectName, gitDir.toString()); JSONObject testTxt = getChild(project, "test.txt"); deleteFile(testTxt);/*from w w w . j ava 2 s . c o m*/ JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT); String gitStatusUri = gitSection.getString(GitConstants.KEY_STATUS); assertStatus(new StatusResult().setMissingNames("test.txt"), gitStatusUri); addFile(testTxt); assertStatus(new StatusResult().setRemoved(1), gitStatusUri); }
From source file:org.eclipse.orion.server.tests.servlets.git.GitAddTest.java
@Test public void testAddAll() throws Exception { URI workspaceLocation = createWorkspace(getMethodName()); String projectName = getMethodName(); JSONObject project = createProjectOrLink(workspaceLocation, projectName, gitDir.toString()); String projectId = project.getString(ProtocolConstants.KEY_ID); JSONObject testTxt = getChild(project, "test.txt"); modifyFile(testTxt, "hello"); String fileName = "new.txt"; WebRequest request = getPostFilesRequest("", getNewFileJSON(fileName).toString(), fileName); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_CREATED, response.getResponseCode()); JSONObject folder = getChild(project, "folder"); JSONObject folderTxt = getChild(folder, "folder.txt"); deleteFile(folderTxt);/* ww w . ja v a 2 s . co m*/ JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT); String gitIndexUri = gitSection.getString(GitConstants.KEY_INDEX); String gitStatusUri = gitSection.getString(GitConstants.KEY_STATUS); assertStatus(new StatusResult().setMissing(1).setModified(1).setUntracked(1), gitStatusUri); request = getPutGitIndexRequest(gitIndexUri /* add all */, null); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); assertStatus(new StatusResult().setAdded(1).setChanged(1).setRemoved(1), gitStatusUri); }