Skip to content

Instantly share code, notes, and snippets.

@tinkerstudent
Created August 6, 2016 21:46
Show Gist options
  • Save tinkerstudent/39a1071073108c3ce7c2891b144be80b to your computer and use it in GitHub Desktop.
Save tinkerstudent/39a1071073108c3ce7c2891b144be80b to your computer and use it in GitHub Desktop.
package com.tinkeracademy.projects;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
public class ChatService {
public static final String URL = "http://tinkercloud-1273.appspot.com/chat?channel=summer2016";
public enum ChatStatus {
YES,
NO,
ERROR
}
public void postToServer(String line)
{
HttpClient client = null;
HttpResponse response = null;
try {
HttpPost post = new HttpPost(URL);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add( new BasicNameValuePair( "chat", line ));
UrlEncodedFormEntity paramEntity = new UrlEncodedFormEntity( params );
post.setEntity(paramEntity);
client = HttpClients.createDefault();
response = client.execute(post);
} catch (Exception e) {
e.printStackTrace();
try {
if (client != null) {
if (client instanceof CloseableHttpClient) {
((CloseableHttpClient) client).close();
}
}
if (response != null) {
if (response instanceof CloseableHttpResponse) {
((CloseableHttpResponse) response).close();
}
}
} catch(IOException e1) {
e1.printStackTrace();
}
}
}
public ArrayList<String> getFromServer()
{
return null;
}
public ChatStatus readLines(File file, ArrayList<String> lines) {
FileReader fileReader = null;
BufferedReader bufReader = null;
try {
fileReader = new FileReader(file);
bufReader = new BufferedReader(fileReader);
String line = bufReader.readLine();
while (line != null) {
lines.add(line);
line = bufReader.readLine();
}
return ChatStatus.YES;
} catch(IOException e) {
e.printStackTrace();
} finally {
try {
if (fileReader != null)
fileReader.close();
if (bufReader != null)
bufReader.close();
} catch(IOException e) {
e.printStackTrace();
}
}
return ChatStatus.ERROR;
}
public ChatStatus writeLine(File file, String line) {
FileWriter fileWriter = null;
PrintWriter printWriter = null;
try {
fileWriter = new FileWriter(file, true);
printWriter = new PrintWriter(fileWriter);
printWriter.println(line);
return ChatStatus.YES;
} catch(IOException e) {
e.printStackTrace();
} finally {
try {
if (fileWriter != null) {
fileWriter.close();
}
if (printWriter != null) {
printWriter.close();
}
} catch(IOException e) {
e.printStackTrace();
}
}
return ChatStatus.ERROR;
}
public ChatStatus createFile(String name) {
String homeDirectory = System.getProperty("user.home");
File file = new File(homeDirectory, name);
try {
boolean created = file.createNewFile();
if (created) {
return ChatStatus.YES;
} else {
return ChatStatus.NO;
}
} catch (IOException e) {
e.printStackTrace();
}
return ChatStatus.ERROR;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment