public JsonElement callRC ( LsApiBody body ) throws LimesurveyRCException { try ( CloseableHttpClient client = HttpClientBuilder . create ( ) . build ( ) ) { HttpPost post = new HttpPost ( url ) ; post . setHeader ( "Content-type" , "application/json" ) ; String jsonBody = gson . toJson ( body ) ; LOGGER . debug ( "API CALL JSON => " + jsonBody ) ; post . setEntity ( new StringEntity ( jsonBody ) ) ; HttpResponse response = client . execute ( post ) ; if ( response . getStatusLine ( ) . getStatusCode ( ) == 200 ) { String jsonResult = EntityUtils . toString ( response . getEntity ( ) ) ; LOGGER . debug ( "API RESPONSE JSON => " + jsonResult ) ; JsonElement result = new JsonParser ( ) . parse ( jsonResult ) . getAsJsonObject ( ) . get ( "result" ) ; if ( result . isJsonObject ( ) && result . getAsJsonObject ( ) . has ( "status" ) ) { throw new LimesurveyRCException ( "Error from API : " + result . getAsJsonObject ( ) . get ( "status" ) . getAsString ( ) ) ; } return result ; } else { throw new LimesurveyRCException ( "Expecting status code 200, got " + response . getStatusLine ( ) . getStatusCode ( ) + " instead" ) ; } } catch ( IOException e ) { throw new LimesurveyRCException ( "Exception while calling API : " + e . getMessage ( ) , e ) ; } }
Call Limesurvey Remote Control .