Skip to content
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 src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
android:versionCode="2"
android:versionName="1.0.0-SNAPSHOT">

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

<application android:name=".Unit2AssessmentApplication">
<activity
android:name=".Unit2AssessmentActivity"
Expand Down
63 changes: 57 additions & 6 deletions src/main/java/nyc/c4q/JSONActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,24 @@
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;
import android.widget.Toast;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

import nyc.c4q.json.Zipcode;

public class JSONActivity extends Activity {

public List<Zipcode> zipcodes;
public boolean storageReadable;
public boolean storageWriteable;
public boolean storageAvailable;
public String results = "";
public final String LOG_TAG = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -42,9 +44,14 @@ protected void onCreate(Bundle savedInstanceState) {
final TextView _lat = (TextView) findViewById(R.id.fieldloclatvalue);
final TextView _long = (TextView) findViewById(R.id.fieldloclongvalue);

storageReadable = isExternalStorageReadable();
storageWriteable = isExternalStorageWritable();

addjson.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


}
});

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

results += "\"_id\":" + _id.getText().toString();
results += "\"pop\":" + pop.getText().toString();
results += "\"city\":" + city.getText().toString();
results += "\"state\":" + state.getText().toString();
results += "\"loc\":" + _lat.getText().toString() + "," + _long.getText().toString();

Log.d(LOG_TAG, results);

/* It's hard for me to understand what this test wants. */


if (storageWriteable) {
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(results.getBytes());
fos.flush();
fos.close();
Toast.makeText(JSONActivity.this, "File saved to: " +
directory + file, Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}

}
});

Expand All @@ -65,4 +97,23 @@ public void onClick(View v) {
}
});
}

// Can we read/write on external storage?
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}

// Can we at least read external storage?
public boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}
}
48 changes: 48 additions & 0 deletions src/main/java/nyc/c4q/ListViewActivity.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package nyc.c4q;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class ListViewActivity extends Activity {
Expand All @@ -19,11 +27,51 @@ public class ListViewActivity extends Activity {
"#bf538d"
};
public TextView textLog;
public EditText adapterCount;

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

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

public class ColorsAdapter extends BaseAdapter {

@Override
public int getCount() {
return 0;
}

@Override
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int position) {
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
/* I know I need to create a custom adapter in order to modify the row views,
* but I haven't been able to do it correctly.
*/

LayoutInflater inflater = getLayoutInflater();
View row = convertView;
row = inflater.inflate(R.layout.listview_tile, parent, false);

for (int i = 0; i < COLORS.length; i++) {
row.setBackgroundColor(Color.parseColor(COLORS[i]));
}
return row;
}
}
}
80 changes: 69 additions & 11 deletions src/main/java/nyc/c4q/NetworkActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,12 @@
import android.widget.Button;
import android.widget.TextView;

import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.HttpUrl;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;

import java.io.BufferedInputStream;
import java.io.DataOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

public class NetworkActivity extends Activity {

Expand All @@ -35,6 +27,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.";
private String LOG_TAG = NetworkActivity.class.getSimpleName();

// Code ===========================

Expand Down Expand Up @@ -76,6 +69,8 @@ protected void onCreate(Bundle savedInstanceState) {
httpbinget.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DownloadWebDataTask task = new DownloadWebDataTask();
task.execute(String.format("https://httpbin.org/get?%s", urlParams));
}
});

Expand Down Expand Up @@ -104,4 +99,67 @@ public void onClick(View v) {
}
});
}

private class DownloadWebDataTask extends AsyncTask<String, Void, String> {

@Override
protected String doInBackground(String... params) {
String response = "";

HttpURLConnection urlConnection = null;
BufferedReader reader = null;

// Check if there is any valid data.
if (params == null) {
return null;
}

try {
URL url = new URL(params[0]);

urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();

InputStream inputStream = urlConnection.getInputStream();
if (inputStream == null) {
return null; // If there's no data, don't process.
}

StringBuffer buffer = new StringBuffer();

reader = new BufferedReader(new InputStreamReader(inputStream));

String line;
while ((line = reader.readLine()) != null) {
buffer.append(line + "\n");
}

// After processing and adding data, return string.
response = buffer.toString();

} catch (IOException e) {
Log.e(LOG_TAG, "Error", e);
} finally {
// Test if connection is open. Disconnect.
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e(LOG_TAG, "Error closing stream", e);
}
}
}

return response;
}

@Override
protected void onPostExecute(String webData) {
httptextlog.setText(webData);
}
}
}
Loading