Skip to content

Instantly share code, notes, and snippets.

@jen6
Created July 14, 2015 03:14
Show Gist options
  • Save jen6/9d0947aa4ae0a430ad80 to your computer and use it in GitHub Desktop.
Save jen6/9d0947aa4ae0a430ad80 to your computer and use it in GitHub Desktop.
class LoginSession {
static String Session = "";
static public String get_session() {
if (Session == "") {
HttpClient http = new DefaultHttpClient();
CookieStore cookieStore = new BasicCookieStore();
String url = "http://makeall.ml:8989/login";
try {
HttpContext context = new BasicHttpContext();
HttpPost httpPost = new HttpPost(url);
ArrayList<NameValuePair> nameValuePairs =
new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("Id", "jen6"));
nameValuePairs.add(new BasicNameValuePair("Pw", "abcd")); //post /파라미터 셋팅
UrlEncodedFormEntity entityRequest =
new UrlEncodedFormEntity(nameValuePairs, "UTF-8");
httpPost.setEntity(entityRequest);
context.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
HttpGet httpGet = new HttpGet("http://makeall.ml:8989/");
HttpResponse response = http.execute(httpGet, context);
CookieStore store = ((DefaultHttpClient) http).getCookieStore();
List<Cookie> cookies = store.getCookies();
if (cookies != null) {
for (Cookie c : cookies) {
Session = c.getValue();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
return Session;
}
}
//글쓰기용
public class BusPostSetter {
public HttpClient http = new DefaultHttpClient();
public CookieStore cookieStore = new BasicCookieStore();
// 쿠키스토어랑 httpclient를 퍼블릭으로 선언
// 프리레퍼런스 사용할 객체 선언
public BusPostSetter() {
}
public BusPostSetter(String Title, String Content, String Want) {
try {
HttpContext context = new BasicHttpContext();
HttpPost httpPost = new HttpPost("http://makeall.ml:8989/board/bus");
httpPost.addHeader("my_session", LoginSession.get_session());
ArrayList<NameValuePair> nameValuePairs =
new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("Title", Title));
nameValuePairs.add(new BasicNameValuePair("Content", Content)); //post 파라미터 셋팅
nameValuePairs.add(new BasicNameValuePair("Want", Want));
UrlEncodedFormEntity entityRequest =
new UrlEncodedFormEntity(nameValuePairs, "UTF-8");
httpPost.setEntity(entityRequest);
HttpResponse responsePost = http.execute(httpPost);
// 리턴값
Log.d("msg", "login");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
class JsonGet {
public static String getJSON(String url, int timeout) {
HttpURLConnection c = null;
try {
URL u = new URL(url);
c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setRequestProperty("Content-length", "0");
c.setUseCaches(false);
c.setAllowUserInteraction(false);
c.setConnectTimeout(timeout);
c.setReadTimeout(timeout);
c.connect();
int status = c.getResponseCode();
switch (status) {
case 200:
case 201:
BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line+"\n");
}
br.close();
return sb.toString();
}
} catch (MalformedURLException ex) {
Log.d("msg", "url exception");
} catch (IOException ex) {
Log.d("msg", "ioexception");
} finally {
if (c != null) {
try {
c.disconnect();
} catch (Exception ex) {
Log.d("msg", "fail in disconnect");
}
}
}
return null;
}
}
class BusList{
int Id, Want;
String Title, Content;
}
class BusListJson {
private int Id, Want;
private String Title, Content;
public void setId(int Id) {
this.Id = Id;
}
public int getId() {
return this.Id;
}
public void setWant(int Want) {
this.Want = Want;
}
public int getWant() {
return this.Want;
}
public void setTitle(String Title) {
this.Title = Title;
}
public String getTitle() {
return this.Title;
}
public void setContent(String Content) {
this.Content = Content;
}
public String getContent() {
return this.Content;
}
}
class BusListGetter{
// URL to get contacts JSON
private String url = "http://makeall.ml:8989/board/buslist/1";
public BusList[] Get(int idx){
String introJson = JsonGet.getJSON(url, 5000);
Log.e("msg", "introJson : " + introJson);
BusListJson[] info_arr = new Gson().fromJson(introJson, BusListJson[].class);
int arr_size = info_arr.length;
Log.d("msg", "arrsize : " + arr_size);
BusList[] arr = new BusList[arr_size];
for(int i = 0; i < arr_size; i++){
BusList bus = new BusList();
bus.Title = info_arr[i].getTitle();
bus.Content = info_arr[i].getContent();
bus.Id = info_arr[i].getId();
bus.Want = info_arr[i].getWant();
arr[i] = bus;
Log.d("msg", arr[i].Title);
}
return arr;
}
}
class Bus{
int Id, WriterId, Want;
String Writer, Title, Content;
}
class BusJson{
private int Id, WriterId, Want;
private String Writer, Title, Content;
public void setId(int Id) {
this.Id = Id;
}
public int getId() {
return this.Id;
}
public void setWriterId(int WriterId) {
this.WriterId = WriterId;
}
public int getWriterId() {
return this.WriterId;
}
public void setWant(int Want) {
this.Want = Want;
}
public int getWant() {
return this.Want;
}
public void setTitle(String Title) {
this.Title = Title;
}
public String getTitle() {
return this.Title;
}
public void setWriter(String Writer) {
this.Writer = Writer;
}
public String getWriter() {
return this.Writer;
}
public void setContent(String Content) {
this.Content = Content;
}
public String getContent() {
return this.Content;
}
}
class BusGetter {
public BusGetter(){}
private String url = "http://makeall.ml:8989/board/bus/";
public Bus Get(int Id){
String json_str = JsonGet.getJSON(url+Id, 5000);
BusJson json_bus = new Gson().fromJson(json_str, BusJson.class);
Log.d("json", json_str);
Bus ret_bus = new Bus();
ret_bus.Id = json_bus.getId();
ret_bus.WriterId = json_bus.getWriterId();
ret_bus.Want = json_bus.getWant();
ret_bus.Title = json_bus.getTitle();
ret_bus.Content = json_bus.getContent();
ret_bus.Writer = json_bus.getWriter();
return ret_bus;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment