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

Marbella #11

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
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
104 changes: 95 additions & 9 deletions src/main/java/nyc/c4q/JSONActivity.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
package nyc.c4q;

import android.app.Activity;
import android.content.Context;
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 org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
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;

Zipcode zp = new Zipcode();
String result="";

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

zipcodes = new ArrayList<Zipcode>();
result = String.valueOf(zipcodes.get(0));

Button savejson = (Button) findViewById(R.id.savejson);
Button loadjson = (Button) findViewById(R.id.loadjson);
Expand All @@ -45,24 +53,102 @@ protected void onCreate(Bundle savedInstanceState) {
addjson.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Read the entire file using getEntireJSON method
// getJSON();
//Parse the file to obtain id, pop, city, etc... using parseJSON method
// parseJSON();
try {
JSONObject zipcode = new JSONObject(result);
zp.setId(zipcode.getString("_id"));
zp.setCity(zipcode.getString("city"));
zp.setPop(zipcode.getInt("pop"));
zp.setState(zipcode.getString("state"));
zp.setLat_(zipcode.getString("-73.939393"));
zp.setLong_(zipcode.getString("40.750316"));

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

_id.setText(zp.getId());
pop.setText(zp.getPop()+"");
city.setText(zp.getCity());
state.setText(zp.getState());
_lat.setText(zp.getLat_());
_long.setText(zp.getLong_());


}
});

//Save to file http://stackoverflow.com/questions/19315316/saving-json-file-from-url-in-internal-storage
savejson.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
File directory = getExternalCacheDir();
File file = new File(directory, "zipcodes.json");
FileOutputStream outputStream;

try {
outputStream = openFileOutput("zipcodes.json", Context.MODE_PRIVATE);
outputStream.write(result.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}

}
});


//open file from gallery/documents
loadjson.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
File directory = getExternalCacheDir();
File file = new File(directory, "zipcodes.json");
try {
BufferedReader bReader = new BufferedReader(new InputStreamReader(openFileInput("zipcodes.json")));
String line;
StringBuffer text = new StringBuffer();
while ((line = bReader.readLine()) != null) {
text.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}

}
});

}


private String getEntireJSON(String urlParams) {
String result= "";
URL url = null;
try {
url = new URL(urlParams);

HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(0);
connection.setReadTimeout(0);

InputStream input = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
StringBuilder sb = new StringBuilder();
String line = "";
while((line = reader.readLine()) !=null){
sb.append(line + "\n");
}
result = sb.toString();
connection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}



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

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

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

@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);
adapterCount.setInputType(InputType.TYPE_CLASS_PHONE);
list = (ListView) findViewById(R.id.list);

ArrayAdapter<String> colors = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, COLORS);

list.setAdapter(colors);

for (int i = 0; i < ListViewActivity.COLORS.length; i++) {
View v = colors.getView(i, null, list);
v.setBackgroundColor(Color.parseColor(COLORS[i]));
}
}
}
100 changes: 92 additions & 8 deletions src/main/java/nyc/c4q/NetworkActivity.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
package nyc.c4q;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.HttpUrl;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
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.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

public class NetworkActivity extends Activity {

Expand Down Expand Up @@ -76,12 +77,48 @@ protected void onCreate(Bundle savedInstanceState) {
httpbinget.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String stringUrl = urlParams;
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
new DownloadTask().execute(stringUrl);
} else {
httptextlog.setText("No Network Available");
}

}
});

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

OkHttpClient client = new OkHttpClient();

Request request = new Request.Builder()
.url(urlParams)
.build();

Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {

}

@Override
public void onResponse(Response response) throws IOException {
//jSON parsing

//update Display
httptextlog.setText(urlParams);


}
});


}
});

Expand All @@ -104,4 +141,51 @@ public void onClick(View v) {
}
});
}


public class DownloadTask extends AsyncTask<String, Void, String> {

@Override
protected String doInBackground(String... urls) {

return downloadUrl(urls[0]);
}

@Override
protected void onPostExecute(String s) {
//want to setText equal to s- show result
httptextlog.setText(urlParams);
}
}

public String downloadUrl(String jsonURl){
String result = "";
try {
URL url = new URL(jsonURl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(0);
connection.setReadTimeout(0);


InputStream input = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));

StringBuilder builder = new StringBuilder();
String line = "";
while ((line = reader.readLine()) != null) {
builder.append(line + "\n");
}
result = builder.toString();
connection.disconnect();

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}


}

Loading