List of usage examples for org.json JSONObject JSONObject
public JSONObject(String source) throws JSONException
From source file:com.wglxy.example.dashL.SearchActivity.java
ArrayList<User> parseJSON(String results) throws JSONException, NullPointerException { ArrayList<User> list = new ArrayList<User>(); JSONObject jsonResult = new JSONObject(results); int status = jsonResult.getInt("status"); int numResults = jsonResult.getInt("numResults"); if (status != 200 && numResults <= 0) return list; else {//from w ww . j a v a 2 s.c o m JSONArray arr = jsonResult.getJSONArray("results"); Log.e(LOG_TAG, "results: " + arr.length()); for (int i = 0; i < arr.length(); i++) { JSONObject jsonObject = arr.getJSONObject(i); User user = new User(); int id = jsonObject.getInt("id"); String email = jsonObject.getString("email"); String first_name = jsonObject.getString("first_name"); String last_name = jsonObject.getString("last_name"); String business_name = jsonObject.getString("business_name"); String services = jsonObject.getString("services"); String address = jsonObject.getString("address"); int stars = jsonObject.getInt("stars"); String image = Constants.API_BASE_IMAGE_URL + jsonObject.getString("image"); Log.e(LOG_TAG, "image: " + image); user.setId(id); user.setEmail(email); user.setFirst_name(first_name); user.setLast_name(last_name); user.setBusiness_name(business_name); user.setAddress(address); user.setServices(services); user.setStars(stars); user.setImage(image); list.add(user); } return list; } }
From source file:net.ccghe.utils.Server.java
public static JSONObject Send(PostDataPairs pairs) { HttpClient client = new DefaultHttpClient(); try {//from ww w . j ava 2 s.c o m HttpPost post = new HttpPost(serverURL); post.setEntity(new UrlEncodedFormEntity(pairs.get())); HttpResponse response = client.execute(post); HttpEntity entity = response.getEntity(); InputStream stream = entity.getContent(); String jsonResponse = convertStreamToString(stream); stream.close(); if (entity != null) { entity.consumeContent(); } JSONObject jObject = new JSONObject(jsonResponse); return jObject; } catch (ClientProtocolException e) { Log.e("EMOCHA", "ClientProtocolException ERR. " + e.getMessage()); } catch (UnknownHostException e) { Log.e("EMOCHA", "UnknownHostException ERR. " + e.getMessage()); } catch (IOException e) { Log.e("EMOCHA", "IOException ERR. " + e.getMessage()); } catch (Exception e) { Log.e("EMOCHA", "Exception ERR. " + e.getMessage()); } return null; }
From source file:game.Clue.JerseyClient.java
public JerseyClient() { String string = ""; try {/*from www. j a va 2 s .c o m*/ //System.out.println("jerseyclient started"); currentgame_state = new JSONObject( "{\"players\":[{\"position\":\"0,3\",\"name\":null,\"active\":\"true\",\"cards\":\"MR GREEN,CANDLESTICK,BALLROOM\",\"character\":\"scarlet\"},{\"position\":\"1,4\",\"name\":null,\"active\":\"true\",\"cards\":\"PROFESSOR PLUM,BILLARD ROOM,ROPE\",\"character\":\"mustard\"},{\"position\":\"4,3\",\"name\":null,\"active\":\"true\",\"cards\":\"DINING ROOM,REVOLVER,WRENCH\",\"character\":\"white\"},{\"position\":\"4,1\",\"name\":null,\"active\":\"true\",\"cards\":\"LIBRARY,COLONEL MUSTARD,STUDY\",\"character\":\"green\"},{\"position\":\"3,0\",\"name\":null,\"active\":\"true\",\"cards\":\"MISS SCARLET,MRS PEACOCK,KNIFE\",\"character\":\"peacock\"},{\"position\":\"1,0\",\"name\":null,\"active\":\"true\",\"cards\":\"HALL,CONSERVATORY,LOUNGE\",\"character\":\"plum\"}],\"move_state\":{\"player\":\"scarlet\",\"moves\":[[\"[0,4], [0,2]\",\"accusation\"]]},\"winner\":null}\"))"); String username = "Mario"; // Step1: Let's 1st read file from fileSystem InputStream crunchifyInputStream = new FileInputStream( "/Users/" + username + "/Documents/JsonTest/JSONFile.txt"); InputStreamReader crunchifyReader = new InputStreamReader(crunchifyInputStream); BufferedReader br = new BufferedReader(crunchifyReader); String line; while ((line = br.readLine()) != null) { string += line + "\n"; } JSONObject jsonObject = new JSONObject(string); //System.out.println(jsonObject); // Step2: Now get JSON File Data from REST Service try { JsonParser Parser; //URL url = new URL("http://192.168.1.7:8080/CluelessServer/webresources/service/game"); URL url = new URL( "http://ec2-54-165-198-60.compute-1.amazonaws.com:8080/CluelessServer/webresources/service/game"); URLConnection connection = url.openConnection(); connection.setDoInput(true); //setDoOutput(true); connection.setRequestProperty("Content-Type", "application/json"); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); // while (in.readLine() != null) { //} System.out.print(in.readLine()); System.out.println("\nREST Service Invoked Successfully..GET Request Sent"); //getGameState(); // sendPUT(ClueGameUI.jTextField2.getText()); //send JSON to Parser //Parser=new JsonParser(in.readLine()); //System.out.println("Parser called"); // sendPUT(); //close connection in.close(); } catch (Exception e) { System.out.println("\nError while calling REST Service"); System.out.println(e); } br.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:game.Clue.JerseyClient.java
public void requestLogintoServer(String name) { try {/* w ww.j a v a 2 s.co m*/ for (int i = 0; i < 6; i++) { JSONObject jsonObject = new JSONObject(name); URL url = new URL( "http://ec2-54-165-198-60.compute-1.amazonaws.com:8080/CluelessServer/webresources/service/player" + i); URLConnection connection = url.openConnection(); connection.setDoOutput(true); //setDoOutput(true); connection.setRequestProperty("PUT", "application/json"); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream()); out.write(jsonObject.toString()); System.out.println("Sent PUT message for logging into server"); out.close(); } } catch (Exception e) { e.printStackTrace(); } }
From source file:game.Clue.JerseyClient.java
public JSONObject getGameState() { JSONObject Object = null;/* ww w . j a v a 2 s. c om*/ try { URL url = new URL( "http://ec2-54-165-198-60.compute-1.amazonaws.com:8080/CluelessServer/webresources/service/player1"); URLConnection connection = url.openConnection(); connection.setDoInput(true); //setDoOutput(true); connection.setRequestProperty("Content-Type", "application/json"); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); Object = new JSONObject(in.readLine()); //; // JsonParser parser=new JsonParser(in.readLine()); // while (in.readLine() != null) { //} // System.out.print(in.readLine()); System.out.println(Object.getJSONArray("players").getJSONObject(0).getString("character")); System.out.println("\nREST Service Invoked Successfully..GET Request Sent"); //sendPUT(); //send JSON to Parser //Parser=new JsonParser(in.readLine()); //System.out.println("Parser called"); // sendPUT(); //close connection // in.close(); } catch (Exception e) { System.out.println("\nError while calling REST Service"); System.out.println(e); } return Object; }
From source file:game.Clue.JerseyClient.java
public void sendPUT(String name) { System.out.println("SendPUT method called"); try {/* www . ja va 2 s.c om*/ JSONObject jsonObject = new JSONObject("{name:" + name + "}"); URL url = new URL( "http://ec2-54-165-198-60.compute-1.amazonaws.com:8080/CluelessServer/webresources/service/game/player1"); URLConnection connection = url.openConnection(); connection.setDoOutput(true); //setDoOutput(true); connection.setRequestProperty("PUT", "application/json"); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream()); out.write(jsonObject.toString()); System.out.println("Sent PUT message to server"); out.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:game.Clue.JerseyClient.java
public void sendPOST() { System.out.println("POST method called"); try {/* w ww . j a v a2 s .c om*/ JSONObject jsonObject = new JSONObject("{player:Brian}"); URL url = new URL( "http://ec2-54-165-198-60.compute-1.amazonaws.com:8080/CluelessServer/webresources/service/game/"); URLConnection connection = url.openConnection(); connection.setDoOutput(true); //setDoOutput(true); connection.setRequestProperty("POST", "application/json"); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream()); System.out.println(jsonObject.toString()); out.write("{" + jsonObject.toString()); System.out.println("Sent PUT message to server"); out.close(); } catch (Exception e) { System.out.println("\nError while calling REST POST Service"); System.out.println(e); } }
From source file:org.wso2.connector.integration.test.base.ConnectorIntegrationTestBase.java
/** * Send HTTP request using {@link HttpURLConnection} in JSON format. * /*from ww w .j a v a2 s . c o m*/ * @param endPoint String End point URL. * @param httpMethod String HTTP method type (GET, POST, PUT etc.) * @param headersMap Map<String, String> Headers need to send to the end point. * @param requestFileName String File name of the file which contains request body data. * @param parametersMap Map<String, String> Additional parameters which is not predefined in the * properties file. * @return RestResponse object. * @throws JSONException * @throws IOException */ protected RestResponse<JSONObject> sendJsonRestRequest(String endPoint, String httpMethod, Map<String, String> headersMap, String requestFileName, Map<String, String> parametersMap) throws IOException, JSONException { HttpURLConnection httpConnection = writeRequest(endPoint, httpMethod, RestResponse.JSON_TYPE, headersMap, requestFileName, parametersMap); String responseString = readResponse(httpConnection); RestResponse<JSONObject> restResponse = new RestResponse<JSONObject>(); restResponse.setHttpStatusCode(httpConnection.getResponseCode()); restResponse.setHeadersMap(httpConnection.getHeaderFields()); if (responseString != null) { JSONObject jsonObject = null; if (isValidJSON(responseString)) { jsonObject = new JSONObject(responseString); } else { jsonObject = new JSONObject(); jsonObject.put("output", responseString); } restResponse.setBody(jsonObject); } return restResponse; }
From source file:org.wso2.connector.integration.test.base.ConnectorIntegrationTestBase.java
/** * Send HTTP request to using {@link HttpsURLConnection} in JSON format. * //from ww w . j a v a 2s .com * @param endPoint String End point URL. * @param httpMethod String HTTP method type (GET, POST, PUT etc.) * @param headersMap Map<String, String> Headers need to send to the end point. * @param requestFileName String File name of the file which contains request body data. * @param parametersMap Map<String, String> Additional parameters which is not predefined in the * properties file. * @param isIgnoreHostVerification boolean flag to ignore host verification. * @return RestResponse object. * @throws JSONException * @throws IOException */ protected RestResponse<JSONObject> sendJsonRestRequestHTTPS(String endPoint, String httpMethod, Map<String, String> headersMap, String requestFileName, Map<String, String> parametersMap, boolean isIgnoreHostVerification) throws IOException, JSONException { HttpsURLConnection httpsConnection = writeRequestHTTPS(endPoint, httpMethod, RestResponse.JSON_TYPE, headersMap, requestFileName, parametersMap, isIgnoreHostVerification); String responseString = readResponseHTTPS(httpsConnection); RestResponse<JSONObject> restResponse = new RestResponse<JSONObject>(); restResponse.setHttpStatusCode(httpsConnection.getResponseCode()); restResponse.setHeadersMap(httpsConnection.getHeaderFields()); if (responseString != null) { JSONObject jsonObject = null; if (isValidJSON(responseString)) { jsonObject = new JSONObject(responseString); } else { jsonObject = new JSONObject(); jsonObject.put("output", responseString); } restResponse.setBody(jsonObject); } return restResponse; }
From source file:org.wso2.connector.integration.test.base.ConnectorIntegrationTestBase.java
/** * Method to validate whether incoming string is parsable as JSON. * //ww w . j a v a 2 s .c om * @param json String to validate. * @return boolean true if incoming JSON is valid, otherwise false. */ private boolean isValidJSON(String json) { try { new JSONObject(json); return true; } catch (JSONException ex) { return false; } }