Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Uuuuuggg" - Charlie Brown #19

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
3 changes: 3 additions & 0 deletions src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
android:versionCode="2"
android:versionName="1.0.0-SNAPSHOT">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application android:name=".Unit2AssessmentApplication">
<activity
android:name=".Unit2AssessmentActivity"
Expand Down
24 changes: 23 additions & 1 deletion src/main/java/nyc/c4q/JSONActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -51,8 +52,27 @@ public void onClick(View v) {
savejson.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
File directory = getExternalCacheDir();
File directory = getExternalCacheDir();
File file = new File(directory, "zipcodes.json");



try {
FileWriter fw = new FileWriter(file);

fw.write(_id.getText().toString());
fw.write(pop.getText().toString());
fw.write(city.getText().toString());
fw.write(state.getText().toString());
fw.write(_lat.getText().toString());
fw.write(_long.getText().toString());
fw.flush();
fw.close();
} catch (IOException e) {
e.printStackTrace();

}

}
});

Expand All @@ -62,6 +82,8 @@ public void onClick(View v) {
public void onClick(View v) {
File directory = getExternalCacheDir();
File file = new File(directory, "zipcodes.json");


}
});
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/nyc/c4q/ListViewActivity.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package nyc.c4q;

import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ListViewActivity extends Activity {
Expand All @@ -19,11 +22,17 @@ public class ListViewActivity extends Activity {
"#bf538d"
};
public TextView textLog;
public ListView colors;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview);
textLog = (TextView) findViewById(R.id.textLog);

colors = (ListView) findViewById(R.id.list);
ArrayAdapter<String> colorAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, COLORS);

colors.setAdapter(colorAdapter);
}
}
87 changes: 85 additions & 2 deletions src/main/java/nyc/c4q/NetworkActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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
Expand All @@ -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."
Expand All @@ -73,21 +85,47 @@ protected void onCreate(Bundle savedInstanceState) {
https://httpbin.org/post
*/

class c4qAsyncGet extends AsyncTask<String, Void, String>{

@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());
}
});

Expand All @@ -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> nameValuePair = new ArrayList<NameValuePair>(2);
nameValuePair.add(new BasicNameValuePair("custname", "james deen"));
nameValuePair.add(new BasicNameValuePair("custtel", "347-8431-6090"));
nameValuePair.add(new BasicNameValuePair("custemail", "[email protected]"));
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 "";
}
}
22 changes: 21 additions & 1 deletion src/main/java/nyc/c4q/NotificationActivity.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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);
}
});

}
}
103 changes: 103 additions & 0 deletions src/main/java/nyc/c4q/json/Zipcode.java
Original file line number Diff line number Diff line change
@@ -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<String> parsed() {
ArrayList<String> zipInfo = new ArrayList<String>();
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<Double> loc(){
ArrayList<Double> zipInfo = new ArrayList<Double>();
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;
}
}
Loading