Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.github.jreddit.utils;
  
  
  import java.io.*;
 import java.net.URL;
This class contains (or will contain) various utilities for jReddit.

Author(s):
Omer Elnour
Raul Rene Lepsa
 
 public class Utils {
 
     private static String userAgent = "Omer's Reddit API Java Wrapper";
 
     public static final String REDDIT_BASE_URL = "http://www.reddit.com";

    
Function that makes a GET request and returns a JSON object

Parameters:
urlPath Path on Reddit to POST to
cookie authentication for the User
Returns:
Object that corresponds to the request
 
     public static Object get(String urlPathString cookie) {
 
         Object object = null;
 
         try {
             URL url = new URL( + urlPath);
             HttpURLConnection connection = (HttpURLConnectionurl.openConnection();
             connection.setUseCaches(false);
             connection.setRequestMethod("GET");
             // Don't pass cookie if it is null
             if (cookie != null) {
                 connection.setRequestProperty("cookie""reddit_session=" + cookie);
             }
             connection.setRequestProperty("User-Agent");
 
 
             // Debugging stuff
             InputStream is;
             Scanner scanner;
             String response;
             if (connection.getResponseCode() != 200) {
                 scanner = new Scanner(connection.getErrorStream());
             } else {
                 is = connection.getInputStream();
                 scanner = new Scanner(is);
             }
             scanner.useDelimiter("\\Z");
             response = "";
             while (scanner.hasNext()) {
                 response += scanner.next();
             }
             scanner.close();
             // Debugging stuff
 
 
             JSONParser parser = new JSONParser();
             object = parser.parse(response);
 
         } catch (IOException e) {
             ..println("Error making GET request to URL path: " + urlPath);
         } catch (ParseException e) {
             ..println("Error parsing response from GET request for URL path: " + urlPath);
         }
 
         return object;
     }

    
Submits a POST request and returns a JSON object that corresponds to it.

Parameters:
urlPath Path on Reddit to POST to
apiParams parameters to put on the request
cookie authentication for the User
Returns:
JSONObject response for the POST
 
     public static JSONObject post(String apiParamsString urlPathString cookie) {
 
         Object response = null;
 
         try {
             URL url = new URL( + urlPath);
             HttpURLConnection connection = (HttpURLConnectionurl.openConnection();
             connection.setDoOutput(true);
             connection.setUseCaches(false);
             connection.setRequestMethod("POST");
             connection.setRequestProperty("Content-Type""application/x-www-form-urlencoded; charset=UTF-8");
             connection.setRequestProperty("Content-Length", String.valueOf(apiParams.length()));
            connection.setRequestProperty("cookie""reddit_session=" + cookie);
            connection.setRequestProperty("User-Agent");
            DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
            outputStream.writeBytes(apiParams);
            outputStream.flush();
            outputStream.close();
            JSONParser parser = new JSONParser();
            response = parser.parse(new BufferedReader(new InputStreamReader(connection.getInputStream())).readLine());
        } catch (IOException e) {
            ..println("Error making POST request to URL path: " + urlPath);
        } catch (ParseException e) {
            ..println("Error parsing response from POST request for URL path: " + urlPath);
        }
        return (JSONObjectresponse;
    }


    
Sets the user agent string. Note: The user-agent identifies the client software originating the HTTP request.

Parameters:
agent User agent string.
    public static void setUserAgent(String agent) {
         = agent;
    }


    
Safely converts an object into string (used because sometimes JSONObject's get() method returns null).

Parameters:
obj The object to convert.
Returns:
The string.
    public static String toString(Object obj) {
        return (obj == null ? null : obj.toString());
    }
New to GrepCode? Check out our FAQ X