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

first commit #24

Open
wants to merge 1 commit 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
2 changes: 2 additions & 0 deletions src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
android:versionCode="2"
android:versionName="1.0.0-SNAPSHOT">

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

<application android:name=".Unit2AssessmentApplication">
<activity
android:name=".Unit2AssessmentActivity"
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/nyc/c4q/ListViewActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,23 @@ public class ListViewActivity extends Activity {
"#bf538d"
};
public TextView textLog;
//ListView listOfColors;

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

//listOfColors = (ListView) findViewById(R.id.list);
//ArrayList<String> colors = new ArrayList<String>();


//ArrayAdapter<String> adapter = new ArrayAdapter<String>(ListViewActivity.this, R.layout.listview_tile,COLORS);
//listOfColors.setAdapter(adapter);


}


}
99 changes: 82 additions & 17 deletions src/main/java/nyc/c4q/NetworkActivity.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,12 @@
package nyc.c4q;

import android.app.Activity;
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.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.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

public class NetworkActivity extends Activity {

// Fields ===========================
Expand Down Expand Up @@ -104,4 +87,86 @@ public void onClick(View v) {
}
});
}
/*
private class getUrlParm extends AsyncTask<String, Void, String>{
String myUrlLink;

protected String doInBackground(String... urls){
try{

}catch(IOException e){

}
}
}
*/

}

/**
* private class DownloadData extends AsyncTask<String, Void,String> {

String myXmlData;

protected String doInBackground(String... urls) { //the ... means it will accept 0 or many urls by deinition with this AsyncTask we can proess more than one thing
try {
myXmlData = downloadXML(urls[0]);

} catch (IOException e) {
return "Unable to download XML file";
}

return "";
}

protected void onPostExecute(String result){
Log.d("OnPostExecute", myXmlData);
xmlData = myXmlData;

}

private String downloadXML(String theUrl) throws IOException { //if there is an error in this method, thorow it back to the calling method and we want that method to deal with it
int BUFFER_SIZE = 2000; //20000 chara at a time.
InputStream is = null; //the mecanism we will use to do the download

String xmlContents = ""; //temp container for our data

try {
URL url = new URL(theUrl); //start opening the url the website address
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //open a link or a a reference to that website
conn.setReadTimeout(10000); //for whatever the reason we cannot download the file we want to close it gracefully. the maximum time to wait for an input stream read before giving up mill sec
conn.setConnectTimeout(15000); //connection timer same reason as before
conn.setRequestMethod("GET"); // get is a stanard way to access data for a web browser
conn.setDoInput(true); //input data
int response = conn.getResponseCode(); //get a response and see what happens
Log.d("DownloadXML", "The response returned is: " + response); //if the response was okay then it would be 200
is = conn.getInputStream();

InputStreamReader isr = new InputStreamReader(is); //read through whatever we send you
int charRead;
char[] inputBuffer = new char[BUFFER_SIZE];
try {
while ((charRead = isr.read(inputBuffer)) > 0)
{ //Reading through the inputBUffer charRead is the number of characters that have been read by this process
String readString = String.copyValueOf(inputBuffer, 0, charRead);//go through the array and we start at 0 and go through however many character have been read

xmlContents += readString; //continually add whatever we read to the xmlContents string
// and then now that we've read that we want to clear out the inputBuffer
inputBuffer = new char[BUFFER_SIZE];
}

return xmlContents;

} catch (IOException e) {
e.printStackTrace(); //shows where it crashed
return null; // if there was a problem then it wont return anything
}

} finally {
if (is != null) //no matter what, if there's an error we still want to execute the code that's in here
is.close(); //whether there is an error or not, make sure we close this InputStream
}
}
}
*
*/
92 changes: 92 additions & 0 deletions src/main/java/nyc/c4q/json/Zipcode.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,96 @@
package nyc.c4q.json;

import android.os.AsyncTask;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

public class Zipcode {
public String _id ="11101";
public String city = "ASTORIA";
public String state = "NY";
public int pop = 23142;

public double[] loc = new double[]{-73.939393,40.750316};


public static final String TAG_ID = "_id";
public static final String TAG_CITY = "city";
public static final String TAG_STATE = "state";
public static final String TAG_POPULATION = "pop";
public static final String TAG_LOCATION = "loc";
public String data;
JSONArray jsonArray = null;
HashMap<String,String> organized = new HashMap<String, String>();


public Zipcode(String data) {
this.data = data;
}

public String get(String data){
return this.data;
}

public String getField(String tag){
new GetInformation().execute();
if(tag == TAG_ID){
return organized.get("Zipcode");
}else if(tag == TAG_CITY){
return organized.get("City");
}else if(tag == TAG_STATE){
return organized.get("State");
}else if( tag == TAG_POPULATION){
return organized.get("Population");
}else{
return null;
}
}



private class GetInformation extends AsyncTask<Void ,Void, Void>{

@Override
protected Void doInBackground(Void... params){

String jsonStr = data;


if(jsonStr != null){
try{
jsonArray = new JSONArray(jsonStr);

for(int i=0; i < jsonArray.length();i++ ){
JSONObject object = jsonArray.getJSONObject(i);

String id = object.getString(TAG_ID);
String city = object.getString(TAG_CITY);
String state = object.getString(TAG_STATE);
String population = object.getString(TAG_POPULATION);
String location = object.getString(TAG_LOCATION);

organized.put("Zipcode",id);
organized.put("City",city);
organized.put("State", state);
organized.put("Population", population);



}


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

return null;
}

}

}
28 changes: 21 additions & 7 deletions src/main/res/layout/activity_listview.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,34 @@

<LinearLayout
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_height= "0dp"
android:layout_weight="1"
android:orientation="horizontal">

<TextView
android:id="@+id/textLog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fill this textview" />
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:text="You have not clicked anything." />

<EditText
android:id="@+id/adapterCount"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:inputType="phone" />

</LinearLayout>

<ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"
android:entries="@array/COLOR" />



</LinearLayout>
19 changes: 19 additions & 0 deletions src/main/res/values/string.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>

<resources>
<string-array name="COLOR">
<item>#142b44</item>
<item>#1d508d</item>
<item>#297cbb</item>
<item>#288ad6</item>
<item>#0fdebd</item>
<item>#16c98d</item>
<item>#feef6d</item>
<item>#ffc83f</item>
<item>#fa5e5b</item>
<item>#bf538d</item>
</string-array>

</resources>