You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
1.1 KiB
Java
38 lines
1.1 KiB
Java
package sh.adb.RandomRedditMemesAPI;
|
|
|
|
import com.github.jreddit.parser.entity.Submission;
|
|
import com.github.jreddit.parser.exception.RedditParseException;
|
|
import com.sun.net.httpserver.HttpExchange;
|
|
import com.sun.net.httpserver.HttpHandler;
|
|
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.OutputStream;
|
|
import java.util.List;
|
|
|
|
class CommunityHandler implements HttpHandler {
|
|
RedditAPI api;
|
|
public CommunityHandler(RedditAPI redditClient) {
|
|
this.api = redditClient;
|
|
}
|
|
|
|
@Override
|
|
public void handle(HttpExchange t) throws IOException {
|
|
//InputStream is = t.getRequestBody();
|
|
//is.read();
|
|
List<Submission> submissions = null;
|
|
try {
|
|
submissions = this.api.getSub("ProgrammerHumor");
|
|
} catch (RedditParseException e) {
|
|
e.printStackTrace();
|
|
}
|
|
assert submissions != null;
|
|
String response = submissions.get(0).getURL();
|
|
System.out.println("response > "+response);
|
|
t.sendResponseHeaders(200, response.length());
|
|
OutputStream os = t.getResponseBody();
|
|
os.write(response.getBytes());
|
|
os.close();
|
|
}
|
|
}
|