Skip to content

Instantly share code, notes, and snippets.

@raulgarreta
Created September 7, 2014 09:07
Show Gist options
  • Save raulgarreta/891d185dce1cc23974c5 to your computer and use it in GitHub Desktop.
Save raulgarreta/891d185dce1cc23974c5 to your computer and use it in GitHub Desktop.
monkeylearn java
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.io.DataOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.lang.StringBuffer;
public class App
{
public static void main( String[] args )
{
URL url;
HttpURLConnection connection = null;
try {
//Create connection
url = new URL("https://api.monkeylearn.com/api/v1/categorizer/mPUywmJH/classify_text/sandbox/");
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Authorization",
"token 6cf55b81e8d69cb6a57ded1b3bdd9d6f1fe2f150");
connection.setUseCaches (false);
connection.setDoInput(true);
connection.setDoOutput(true);
//Send request
DataOutputStream wr = new DataOutputStream (
connection.getOutputStream ());
String urlParameters =
"text=" + URLEncoder.encode("some text to test", "UTF-8");
wr.writeBytes (urlParameters);
wr.flush ();
wr.close ();
//Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
System.out.println(response.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
if(connection != null) {
connection.disconnect();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment