Code example for JsonParser

Methods: parse

0
			} 
		return (new Gson()).fromJson(json.eval(responseName), collectionType);
	} 
 
	public JsonAccessor execute(CloudStackCommand cmd, String apiKey, String secretKey) throws Exception {
		JsonParser parser = new JsonParser();
		URL url = new URL(_serviceUrl + cmd.signCommand(apiKey, secretKey));
		 
		if(logger.isDebugEnabled()) 
			logger.debug("Cloud API call + [" + url.toString() + "]");
		 
        URLConnection connect = url.openConnection();
         
        int statusCode;
        statusCode = ((HttpURLConnection)connect).getResponseCode();
        if(statusCode >= 400) {
        	logger.error("Cloud API call + [" + url.toString() + "] failed with status code: " + statusCode);
            String errorMessage = ((HttpURLConnection)connect).getResponseMessage();
            if(errorMessage == null){
                errorMessage = connect.getHeaderField("X-Description");
            } 
             
            if(errorMessage == null){
                errorMessage = "CloudStack API call HTTP response error, HTTP status code: " + statusCode;
            } 
 
        	throw new IOException(errorMessage);
        } 
         
        InputStream inputStream = connect.getInputStream(); 
		JsonElement jsonElement = parser.parse(new InputStreamReader(inputStream));
		if(jsonElement == null) {
        	logger.error("Cloud API call + [" + url.toString() + "] failed: unable to parse expected JSON response");
        	 
        	throw new IOException("CloudStack API call error : invalid JSON response");
		}