diff --git a/build.gradle b/build.gradle index 7c6b66b..52c294f 100644 --- a/build.gradle +++ b/build.gradle @@ -45,6 +45,10 @@ dependencies { // https://github.com/robolectric/robolectric/issues/1369 compile 'com.android.support:support-v4:22.1.1' + // OkHTTP + compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0' + compile 'com.squareup.okhttp:okhttp:2.0.0' + // why is this 'compile' over 'testCompile'? Man... so weird. // relevant: https://github.com/square/assertj-android/issues/129 compile 'com.squareup.assertj:assertj-android:1.0.0' diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index ea076f1..9dc71bc 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -4,6 +4,9 @@ android:versionCode="2" android:versionName="1.0.0-SNAPSHOT"> + + + colorAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, COLORS); + + colors.setAdapter(colorAdapter); } } diff --git a/src/main/java/nyc/c4q/NetworkActivity.java b/src/main/java/nyc/c4q/NetworkActivity.java index 3604cfc..99a7664 100644 --- a/src/main/java/nyc/c4q/NetworkActivity.java +++ b/src/main/java/nyc/c4q/NetworkActivity.java @@ -16,13 +16,25 @@ import com.squareup.okhttp.RequestBody; import com.squareup.okhttp.Response; +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.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.message.BasicNameValuePair; + import java.io.BufferedInputStream; import java.io.DataOutputStream; +import java.io.IOException; import java.io.InputStream; +import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; +import java.util.List; public class NetworkActivity extends Activity { @@ -35,7 +47,7 @@ public class NetworkActivity extends Activity { public Button httpbinpostokhttp; public Button cleartextlog; final public String urlParams = "custname=james+dean&custtel=347-841-6090&custemail=hello%40c4q.nyc&size=small&topping=cheese&delivery=18%3A15&comments=Leave+it+by+the+garage+door.+Don't+ask+any+questions."; - + OkHttpClient okHttpClient = new OkHttpClient(); // Code =========================== @Override @@ -55,7 +67,7 @@ protected void onCreate(Bundle savedInstanceState) { Shortcut to create URL in Java: String.format("https://httpbin.org/get?%s", urlParams); - + /* HTTP GET request we'll be using: $ curl "https://httpbin.org/get?custname=james+dean&custtel=347-841-6090&custemail=hello%40c4q.nyc&size=small&topping=cheese&delivery=18%3A15&comments=Leave+it+by+the+garage+door.+Don%27t+ask+any+questions." @@ -73,21 +85,47 @@ protected void onCreate(Bundle savedInstanceState) { https://httpbin.org/post */ + class c4qAsyncGet extends AsyncTask{ + + @Override + protected String doInBackground(String... strings) { + String url = String.format("https://httpbin.org/get?%s", urlParams); + try { + HttpClient client = new DefaultHttpClient(); + HttpGet httpGet = new HttpGet(url); + HttpResponse httpResponse = client.execute(httpGet); + + }catch (Exception ex) { + ex.printStackTrace(); + } + postRequest(); + return null; + } + } + httpbinget.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { + httptextlog.setText(urlParams); } }); httpbingetokhttp.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { + try { + httptextlog.setText(okHTTPGet(String.format("https://httpbin.org/get?%s", urlParams))); + } catch (IOException ex) { + ex.printStackTrace(); + } } }); httpbinpost.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { + + httptextlog.setText(postRequest()); } }); @@ -104,4 +142,49 @@ public void onClick(View v) { } }); } + public String okHTTPGet(String url) throws IOException{ + Request request = new Request.Builder() + .url(url) + .build(); + + Response response = okHttpClient.newCall(request).execute(); + return response.body().string(); + } + public String postRequest(){ + + HttpClient httpClient = new DefaultHttpClient(); + HttpPost httpPost = new HttpPost("https://httpbin.org/post"); + + + //Post Data + List nameValuePair = new ArrayList(2); + nameValuePair.add(new BasicNameValuePair("custname", "james deen")); + nameValuePair.add(new BasicNameValuePair("custtel", "347-8431-6090")); + nameValuePair.add(new BasicNameValuePair("custemail", "hello@c4q.nyc")); + nameValuePair.add(new BasicNameValuePair("size", "small")); + nameValuePair.add(new BasicNameValuePair("topping", "cheese")); + nameValuePair.add(new BasicNameValuePair("delivery", "22:15")); + nameValuePair.add(new BasicNameValuePair("comments", "Leave it by the garage door. Don't ask any questions.")); + + + //Encoding POST data + try { + httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair)); + } catch (UnsupportedEncodingException e) { + // log exception + e.printStackTrace(); + } + + //making POST request. + try { + HttpResponse response = httpClient.execute(httpPost); + // write response to log + Log.d("Http Post Response:", response.toString()); + return response.toString(); + } catch (Exception e) { + // Log exception + e.printStackTrace(); + } + return ""; + } } diff --git a/src/main/java/nyc/c4q/NotificationActivity.java b/src/main/java/nyc/c4q/NotificationActivity.java index f1f56ad..13f74ce 100644 --- a/src/main/java/nyc/c4q/NotificationActivity.java +++ b/src/main/java/nyc/c4q/NotificationActivity.java @@ -1,8 +1,13 @@ package nyc.c4q; import android.app.Activity; +import android.app.Notification; import android.app.NotificationManager; +import android.app.PendingIntent; +import android.content.Context; +import android.content.Intent; import android.os.Bundle; +import android.view.View; import android.widget.Button; public class NotificationActivity extends Activity { @@ -23,7 +28,22 @@ protected void onCreate(Bundle savedInstanceState) { Button swipenotification = (Button) findViewById(R.id.swipenotification); Button permanentnotification = (Button) findViewById(R.id.permanentnotification); Button dismisspermanentnotification = (Button) findViewById(R.id.dismisspermanentnotification); - Button buttonnotification = (Button) findViewById(R.id.buttonnotification); + final Button buttonnotification = (Button) findViewById(R.id.buttonnotification); + + buttonnotification.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + String subject = "subject"; + String body = "body"; + + NotificationManager notif=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); + Notification notify=new Notification(R.drawable.c4qfavicon,buttonnotification.getText().toString(),System.currentTimeMillis()); + PendingIntent pending= PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0); + + notify.setLatestEventInfo(getApplicationContext(),subject,body,pending); + notif.notify(0, notify); + } + }); } } diff --git a/src/main/java/nyc/c4q/json/Zipcode.java b/src/main/java/nyc/c4q/json/Zipcode.java index 6d4761f..c479754 100644 --- a/src/main/java/nyc/c4q/json/Zipcode.java +++ b/src/main/java/nyc/c4q/json/Zipcode.java @@ -1,4 +1,107 @@ package nyc.c4q.json; +import com.google.gson.JsonParser; + +import org.json.JSONArray; +import org.json.JSONObject; + +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.util.ArrayList; +import java.util.Iterator; + public class Zipcode { + + String _id = parsed().get(0); + String city = parsed().get(1); + String state = parsed().get(2); + int pop = pop(); + double _lat = loc().get(0); + double _long = loc().get(1); + + + public ArrayList parsed() { + ArrayList zipInfo = new ArrayList(); + JsonParser jsonParser = new JsonParser(); + + try { + Object obj = jsonParser.parse(new FileReader("/Users/c4q-anthony-mcbride/Desktop/Exercises/unit-2-assessment/src/main/res/raw/zips.json")); + + JSONArray jsonArray = (JSONArray) obj; + JSONObject jsonObject; + + + for (int i = 0; i < jsonArray.length(); i++) { + if (jsonArray.get(i).toString().contains("11101")) { + jsonObject = (JSONObject) jsonArray.get(i); + zipInfo.add(jsonObject.get("_id").toString()); + zipInfo.add(jsonObject.get("city").toString()); + zipInfo.add(jsonObject.get("state").toString()); + return zipInfo; + } + } + + + } catch (Exception ex) { + ex.printStackTrace(); + } + return zipInfo; + } + + public int pop() { + int pop = 0; + + JsonParser jsonParser = new JsonParser(); + + try { + Object obj = jsonParser.parse(new FileReader("/Users/c4q-anthony-mcbride/Desktop/Exercises/unit-2-assessment/src/main/res/raw/zips.json")); + + JSONArray jsonArray = (JSONArray) obj; + JSONObject jsonObject; + + + for (int i = 0; i < jsonArray.length(); i++) { + if (jsonArray.get(i).toString().contains("11101")) { + JSONObject population = (JSONObject) jsonArray.get(i); + pop = population.getInt("pop"); + return pop; + } + + } + } catch (Exception ex) { + ex.printStackTrace(); + } + + + return pop; + } + public ArrayList loc(){ + ArrayList zipInfo = new ArrayList(); + JsonParser jsonParser = new JsonParser(); + + try { + Object obj = jsonParser.parse(new FileReader("/Users/c4q-anthony-mcbride/Desktop/Exercises/unit-2-assessment/src/main/res/raw/zips.json")); + + JSONArray jsonArray = (JSONArray) obj; + JSONObject jsonObject; + + + for (int i = 0; i < jsonArray.length(); i++) { + if (jsonArray.get(i).toString().contains("11101")) { + JSONArray locs = (JSONArray) jsonArray.get(i); + JSONObject lat = (JSONObject) locs.get(0); + JSONObject lon = (JSONObject) locs.get(1); + + zipInfo.add(locs.getDouble(0)); + zipInfo.add(locs.getDouble(1)); + return zipInfo; + } + } + + + } catch (Exception ex) { + ex.printStackTrace(); + } + return zipInfo; + } } diff --git a/src/main/res/layout/activity_listview.xml b/src/main/res/layout/activity_listview.xml index 0d4b9d6..bfd14b8 100644 --- a/src/main/res/layout/activity_listview.xml +++ b/src/main/res/layout/activity_listview.xml @@ -9,20 +9,33 @@ + android:layout_width="match_parent" + android:layout_height="0dp" + + android:orientation="horizontal" + android:layout_weight="1"> + + + android:layout_width="0dp" + android:layout_height="match_parent" + + android:text="@string/Test4String" + android:layout_weight="3" /> + android:layout_width="match_parent" + android:layout_height="0dp" + android:layout_weight="9" /> \ No newline at end of file diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml new file mode 100644 index 0000000..cf17cec --- /dev/null +++ b/src/main/res/values/strings.xml @@ -0,0 +1,5 @@ + + + You have not clicked anything. + + \ No newline at end of file