diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 9a8b7e5..96cc43e 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -1,7 +1,6 @@ - - + diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 94a25f7..6564d52 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/app/app.iml b/app/app.iml index e8dc66b..19dfb88 100644 --- a/app/app.iml +++ b/app/app.iml @@ -114,49 +114,50 @@ - - + + + - - - - - - - - - - - - - - - - + - + + - - - - - + - - + + + + - - - - - - + - - + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 2c6a478..f3a3c85 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -20,7 +20,7 @@ android { } dependencies { - compile fileTree(dir: 'libs', include: ['*.jar']) + compile fileTree(include: ['*.jar'], dir: 'libs') compile 'com.android.support:appcompat-v7:23.0.1' compile files('libs/YouTubeAndroidPlayerApi.jar') compile 'com.google.android.gms:play-services:8.1.0' @@ -32,4 +32,5 @@ dependencies { // Image loading library compile 'com.squareup.picasso:picasso:2.3.+' compile files('libs/gson-2.4.jar') + compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2' } diff --git a/app/src/main/java/edu/phambdvcu/hacknc/Client.java b/app/src/main/java/edu/phambdvcu/hacknc/Client.java index 59f9efb..a9d78d0 100644 --- a/app/src/main/java/edu/phambdvcu/hacknc/Client.java +++ b/app/src/main/java/edu/phambdvcu/hacknc/Client.java @@ -1,7 +1,166 @@ package edu.phambdvcu.hacknc; -/** - * Created by phambd on 10/10/15. - */ +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.StatusLine; +import org.apache.http.client.HttpClient; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.params.BasicHttpParams; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import android.os.AsyncTask; +import android.os.Bundle; +import android.app.Activity; +import android.util.Log; +import android.view.Menu; +import android.widget.Toast; + public class Client { + + public Client(){ + } + + public void post(VideoItem v, Responder R){ + PostFetcher fetcher = new PostFetcher(v,R); + fetcher.execute(); + } + public void get(Responder R){ + GetFetcher fetcher = new GetFetcher(R); + fetcher.execute(); + } + + + private class PostFetcher extends AsyncTask { + private static final String TAG = "PostFetcher"; + public static final String SERVER_URL = "https://castbox-1094.appspot.com/Video"; + + Responder R; + VideoItem v; + + public PostFetcher(VideoItem v, Responder R){ + this.R = R; + this.v = v; + + + } + + @Override + protected String doInBackground(Void... params) { + try { + //Create an HTTP client + HttpClient client = new DefaultHttpClient(); + HttpPost post = new HttpPost(SERVER_URL); + post.setEntity(new UrlEncodedFormEntity(v.toMap())); + + //Perform the request and check the status code + HttpResponse response = client.execute(post); + StatusLine statusLine = response.getStatusLine(); + if(statusLine.getStatusCode() == 200) { + HttpEntity entity = response.getEntity(); + InputStream content = entity.getContent(); + + try { + //Read the server response and attempt to parse it as JSON + Reader reader = new InputStreamReader(content); + + GsonBuilder gsonBuilder = new GsonBuilder(); + Gson gson = gsonBuilder.create(); + List videos = new ArrayList(); + videos = Arrays.asList(gson.fromJson(reader, VideoItem[].class)); + content.close(); + + R.respond(videos); + } catch (Exception ex) { + Log.e(TAG, "Failed to parse JSON due to: " + ex); + + } + } else { + Log.e(TAG, "Server responded with status code: " + statusLine.getStatusCode()); + + } + } catch(Exception ex) { + Log.e(TAG, "Failed to send HTTP POST request due to: " + ex); + + } + return null; + } + } + private class GetFetcher extends AsyncTask { + private static final String TAG = "PostFetcher"; + public static final String SERVER_URL = "https://castbox-1094.appspot.com/Video"; + + Responder R; + VideoItem v; + + public GetFetcher(Responder R){ + this.R = R; + } + + @Override + protected String doInBackground(Void... params) { + try { + //Create an HTTP client + HttpClient client = new DefaultHttpClient(); + HttpGet get = new HttpGet(SERVER_URL); + + get.setParams(VideoItem.getKey()); + + //Perform the request and check the status code + HttpResponse response = client.execute(get); + StatusLine statusLine = response.getStatusLine(); + if(statusLine.getStatusCode() == 200) { + HttpEntity entity = response.getEntity(); + InputStream content = entity.getContent(); + + try { + //Read the server response and attempt to parse it as JSON + Reader reader = new InputStreamReader(content); + + GsonBuilder gsonBuilder = new GsonBuilder(); + Gson gson = gsonBuilder.create(); + List videos = new ArrayList(); + videos = Arrays.asList(gson.fromJson(reader, VideoItem[].class)); + content.close(); + + R.respond(videos); + } catch (Exception ex) { + Log.e(TAG, "Failed to parse JSON due to: " + ex); + + } + } else { + Log.e(TAG, "Server responded with status code: " + statusLine.getStatusCode()); + } + } catch(Exception ex) { + Log.e(TAG, "Failed to send HTTP POST request due to: " + ex); + + } + return null; + } + } + + public static void main(String []args){ + + VideoItem v = new VideoItem(); + v.setCurrent(true); + v.setId("butt"); + v.setFinished(true); + v.setRating(5); + v.setThumbnailURL("pic"); + v.setTitle("butthole"); + Client c = new Client(); + c.post(v,new Tester()); + + } + + } diff --git a/app/src/main/java/edu/phambdvcu/hacknc/Responder.java b/app/src/main/java/edu/phambdvcu/hacknc/Responder.java new file mode 100644 index 0000000..bd5e5ce --- /dev/null +++ b/app/src/main/java/edu/phambdvcu/hacknc/Responder.java @@ -0,0 +1,12 @@ +package edu.phambdvcu.hacknc; + +/** + * Created by willem on 10/10/15. + */ + + +public interface Responder { + final String SECRET_KEY = "Mr. Poopie Butthole"; + + String respond(Object o); +} diff --git a/app/src/main/java/edu/phambdvcu/hacknc/Tester.java b/app/src/main/java/edu/phambdvcu/hacknc/Tester.java new file mode 100644 index 0000000..a2787a3 --- /dev/null +++ b/app/src/main/java/edu/phambdvcu/hacknc/Tester.java @@ -0,0 +1,12 @@ +package edu.phambdvcu.hacknc; + +/** + * Created by willem on 10/11/15. + */ +public class Tester implements Responder { + + public String respond(Object o){ + System.out.print(o.toString()); + return o.toString(); + } +} diff --git a/app/src/main/java/edu/phambdvcu/hacknc/VideoItem.java b/app/src/main/java/edu/phambdvcu/hacknc/VideoItem.java index 7add8f7..ba507b5 100644 --- a/app/src/main/java/edu/phambdvcu/hacknc/VideoItem.java +++ b/app/src/main/java/edu/phambdvcu/hacknc/VideoItem.java @@ -1,12 +1,49 @@ package edu.phambdvcu.hacknc; + + +import org.apache.http.NameValuePair; +import org.apache.http.message.BasicNameValuePair; +import org.apache.http.params.BasicHttpParams; + +import java.util.ArrayList; + /** * Created by phambd on 10/10/15. */ + public class VideoItem { private String title; private String thumbnailURL; private String id; + private int rating; + private boolean finished, current; + + public int getRating() { + return rating; + } + + public boolean isFinished() { + return finished; + } + + public boolean isCurrent() { + return current; + } + + public void setCurrent(boolean current) { + this.current = current; + } + + public void setRating(int rating) { + this.rating = rating; + } + + public void setFinished(boolean finished) { + this.finished = finished; + } + + public String getId() { return id; @@ -32,5 +69,33 @@ public void setThumbnailURL(String thumbnail) { this.thumbnailURL = thumbnail; } + public ArrayList toMap(){ + ArrayList postParameters = new ArrayList(); + + + postParameters.add(new BasicNameValuePair("title", title)); + postParameters.add(new BasicNameValuePair("rating",new Integer(rating).toString())); + postParameters.add(new BasicNameValuePair("thumbnail", thumbnailURL)); + postParameters.add(new BasicNameValuePair("id", id)); + postParameters.add(new BasicNameValuePair("finished",new Boolean(finished).toString())); + postParameters.add(new BasicNameValuePair("current",new Boolean(current).toString())); + postParameters.add(new BasicNameValuePair("key", Responder.SECRET_KEY)); + + return postParameters; + + + + + } + + public static BasicHttpParams getKey(){ + + BasicHttpParams params = new BasicHttpParams(); + params.setParameter("key", Responder.SECRET_KEY); + return params; + } + + + }