public void sendRequest ( ) throws IOException { /* Create & Format URL */ URL url = new URL ( CleverBotQuery . formatRequest ( CleverBotQuery . URL_STRING , this . key , this . phrase , this . conversationID ) ) ; /* Open Connection */ URLConnection urlConnection = url . openConnection ( ) ; /* Read input */ BufferedReader in = new BufferedReader ( new InputStreamReader ( urlConnection . getInputStream ( ) ) ) ; String inputLine = in . readLine ( ) ; /* Create JSON Object */ JsonObject jsonObject = new JsonParser ( ) . parse ( inputLine ) . getAsJsonObject ( ) ; // convert to JSON /* Get params */ this . setConversationID ( jsonObject . get ( "cs" ) . getAsString ( ) ) ; // update conversation ID this . setResponse ( jsonObject . get ( "output" ) . getAsString ( ) ) ; // get output this . setRandomNumber ( Integer . parseInt ( jsonObject . get ( "random_number" ) . getAsString ( ) ) ) ; // get output in . close ( ) ; // close! }
Sends request to CleverBot servers . API key and phrase should be set prior to this call