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

Ramona #26

Open
wants to merge 4 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
1 change: 1 addition & 0 deletions c:\user.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"mkyong","age":29,"messages":["msg 1","msg 2","msg 3"]}
3 changes: 3 additions & 0 deletions src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
package="nyc.c4q"
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
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/nyc/c4q/ColorAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package nyc.c4q;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

/**
* Created by Ramona Harrison
* on 6/28/15.
*/
public class ColorAdapter<String> extends ArrayAdapter<String> {
TextView textLog;
// View lookup cache
private static class ViewHolder {
LinearLayout background;

}

public ColorAdapter(Context context, int layout, ArrayList<String> colors, TextView textLog) {
super(context, layout, colors);
this.textLog = textLog;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// Get the data item for this position
String color = getItem(position);

// Inflate the view
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.custom_list_item, parent, false);
viewHolder.background = (LinearLayout) convertView.findViewById(R.id.background);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}

// Set view background color
viewHolder.background.setBackgroundColor(Color.parseColor((java.lang.String) color));

return convertView;
}
}
95 changes: 95 additions & 0 deletions src/main/java/nyc/c4q/HttpUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package nyc.c4q;

import android.os.Handler;
import android.os.Looper;

import com.squareup.okhttp.Callback;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;

import java.io.IOException;

import okio.BufferedSink;

public class HttpUtil {

private OkHttpClient client;
private Request.Builder builder;

public void get(String url, HttpCallback cb) {
call("GET", url, cb);
}

public void post(String url, HttpCallback cb) {
call("POST", url, cb);
}

private void call(String method, String url, final HttpCallback cb) {
Request request = builder.url(url).method(method, method.equals("GET") ? null : new RequestBody() {

@Override
public MediaType contentType() {
return null;
}

@Override
public void writeTo(BufferedSink sink) throws IOException {

}
}).build();

client.newCall(request).enqueue(new Callback() {
Handler mainHandler = new Handler(Looper.getMainLooper());

@Override
public void onFailure(Request request,final IOException throwable) {
mainHandler.post(new Runnable() {

@Override
public void run() {
cb.onFailure(null, throwable);
}
});

}

@Override
public void onResponse(final Response response) throws IOException {
mainHandler.post(new Runnable() {

@Override
public void run() {
if (!response.isSuccessful()) {
cb.onFailure(response, null);
return;
}
cb.onSuccess(response);
}
});

}
});
}


public interface HttpCallback {

/**
* called when the server response was not 2xx or when an exception was thrown in the process
* @param response - in case of server error (4xx, 5xx) this contains the server response
* in case of IO exception this is null
* @param throwable - contains the exception. in case of server error (4xx, 5xx) this is null
*/
public void onFailure(Response response, IOException throwable);

/**
* contains the server response
* @param response
*/
public void onSuccess(Response response);
}

}
102 changes: 92 additions & 10 deletions src/main/java/nyc/c4q/JSONActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,45 @@

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.stream.JsonWriter;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
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 Zipcode zipcode;
Gson gson;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_json);

zipcodes = new ArrayList<Zipcode>();
gson = new Gson();

Button savejson = (Button) findViewById(R.id.savejson);
Button loadjson = (Button) findViewById(R.id.loadjson);
Button addjson = (Button) findViewById(R.id.addjson);
final Button savejson = (Button) findViewById(R.id.savejson);
final Button loadjson = (Button) findViewById(R.id.loadjson);
final Button addjson = (Button) findViewById(R.id.addjson);

final TextView _id = (TextView) findViewById(R.id.field_idvalue);
final TextView pop = (TextView) findViewById(R.id.fieldpopvalue);
Expand All @@ -45,6 +52,20 @@ protected void onCreate(Bundle savedInstanceState) {
addjson.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

// build zipcode
String _idS = _id.getText().toString();
String cityS = city.getText().toString();
double latS = Double.valueOf(_lat.getText().toString());
double longS = Double.valueOf(_long.getText().toString());
double[] location = {latS, longS};
String stateS = state.getText().toString();
int popS = Integer.valueOf(pop.getText().toString());

Zipcode zip = new Zipcode(_idS, cityS, location, popS, stateS);

// add it to the list
zipcodes.add(zip);
}
});

Expand All @@ -53,16 +74,77 @@ public void onClick(View v) {
public void onClick(View v) {
File directory = getExternalCacheDir();
File file = new File(directory, "zipcodes.json");
try {
FileOutputStream fout = new FileOutputStream(file);
writeJsonStream(fout, zipcodes);
} catch (Exception e) {
e.printStackTrace();
}


}
});


loadjson.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
File directory = getExternalCacheDir();
File file = new File(directory, "zipcodes.json");
loadJson();
}
});
}

public void loadJson() {

File directory = getExternalCacheDir();
File file = new File(directory, "zipcodes.json");
JsonParser parser = new JsonParser();
JsonArray jsonArray = null;
try {
jsonArray = (JsonArray) parser.parse(new FileReader(file));
populateList(jsonArray);

} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

public void populateList(JsonArray jsonArray) {
for (JsonElement zipcode : jsonArray) {
zipcodes.add(gson.fromJson(zipcode, Zipcode.class));
}
}

public void writeJsonStream(OutputStream out, List<Zipcode> zipcodes) throws IOException {
JsonWriter writer = new JsonWriter(new OutputStreamWriter(out, "UTF-8"));
writeMessagesArray(writer, zipcodes);
writer.close();
}

public void writeMessagesArray(JsonWriter writer, List<Zipcode> zipcodes) throws IOException {
writer.beginArray();
for (Zipcode zipcode : zipcodes) {
writeMessage(writer, zipcode);
}
writer.endArray();
}

public void writeMessage(JsonWriter writer, Zipcode message) throws IOException {
writer.beginObject();
writer.name("_id").value(message.get_id());
writer.name("city").value(message.getCity());
writer.name("loc");
writeDoublesArray(writer, message.getLoc());
writer.name("pop").value(message.getPop());
writer.name("state").value(message.getState());
writer.endObject();
}


public void writeDoublesArray(JsonWriter writer, double[] loc) throws IOException {
writer.beginArray();
writer.value(loc[0]);
writer.value(loc[1]);
writer.endArray();
}
}
Loading