From a91295d69541438c6786fed86b03d20df9ee378c Mon Sep 17 00:00:00 2001 From: kmcnee Date: Thu, 25 Feb 2016 09:10:59 -0500 Subject: [PATCH] Kyle McNee --- NetworkingLab/.gitignore | 8 + NetworkingLab/.idea/.name | 1 + NetworkingLab/.idea/compiler.xml | 22 +++ .../.idea/copyright/profiles_settings.xml | 3 + NetworkingLab/.idea/encodings.xml | 6 + NetworkingLab/.idea/gradle.xml | 19 +++ NetworkingLab/.idea/misc.xml | 62 +++++++ NetworkingLab/.idea/modules.xml | 9 + NetworkingLab/.idea/runConfigurations.xml | 12 ++ NetworkingLab/.idea/vcs.xml | 6 + NetworkingLab/app/.gitignore | 1 + NetworkingLab/app/build.gradle | 26 +++ NetworkingLab/app/proguard-rules.pro | 17 ++ .../networkinglab/ApplicationTest.java | 13 ++ .../app/src/main/AndroidManifest.xml | 23 +++ .../kylemcnee/networkinglab/MainActivity.java | 154 +++++++++++++++++ .../app/src/main/res/layout/activity_main.xml | 34 ++++ .../src/main/res/mipmap-hdpi/ic_launcher.png | Bin 0 -> 3418 bytes .../src/main/res/mipmap-mdpi/ic_launcher.png | Bin 0 -> 2206 bytes .../src/main/res/mipmap-xhdpi/ic_launcher.png | Bin 0 -> 4842 bytes .../main/res/mipmap-xxhdpi/ic_launcher.png | Bin 0 -> 7718 bytes .../main/res/mipmap-xxxhdpi/ic_launcher.png | Bin 0 -> 10486 bytes .../app/src/main/res/values-w820dp/dimens.xml | 6 + .../app/src/main/res/values/colors.xml | 6 + .../app/src/main/res/values/dimens.xml | 5 + .../app/src/main/res/values/strings.xml | 3 + .../app/src/main/res/values/styles.xml | 11 ++ .../networkinglab/ExampleUnitTest.java | 15 ++ NetworkingLab/build.gradle | 23 +++ NetworkingLab/gradle.properties | 18 ++ .../gradle/wrapper/gradle-wrapper.jar | Bin 0 -> 53637 bytes .../gradle/wrapper/gradle-wrapper.properties | 6 + NetworkingLab/gradlew | 160 ++++++++++++++++++ NetworkingLab/gradlew.bat | 90 ++++++++++ NetworkingLab/settings.gradle | 1 + 35 files changed, 760 insertions(+) create mode 100644 NetworkingLab/.gitignore create mode 100644 NetworkingLab/.idea/.name create mode 100644 NetworkingLab/.idea/compiler.xml create mode 100644 NetworkingLab/.idea/copyright/profiles_settings.xml create mode 100644 NetworkingLab/.idea/encodings.xml create mode 100644 NetworkingLab/.idea/gradle.xml create mode 100644 NetworkingLab/.idea/misc.xml create mode 100644 NetworkingLab/.idea/modules.xml create mode 100644 NetworkingLab/.idea/runConfigurations.xml create mode 100644 NetworkingLab/.idea/vcs.xml create mode 100644 NetworkingLab/app/.gitignore create mode 100644 NetworkingLab/app/build.gradle create mode 100644 NetworkingLab/app/proguard-rules.pro create mode 100644 NetworkingLab/app/src/androidTest/java/com/example/kylemcnee/networkinglab/ApplicationTest.java create mode 100644 NetworkingLab/app/src/main/AndroidManifest.xml create mode 100644 NetworkingLab/app/src/main/java/com/example/kylemcnee/networkinglab/MainActivity.java create mode 100644 NetworkingLab/app/src/main/res/layout/activity_main.xml create mode 100644 NetworkingLab/app/src/main/res/mipmap-hdpi/ic_launcher.png create mode 100644 NetworkingLab/app/src/main/res/mipmap-mdpi/ic_launcher.png create mode 100644 NetworkingLab/app/src/main/res/mipmap-xhdpi/ic_launcher.png create mode 100644 NetworkingLab/app/src/main/res/mipmap-xxhdpi/ic_launcher.png create mode 100644 NetworkingLab/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png create mode 100644 NetworkingLab/app/src/main/res/values-w820dp/dimens.xml create mode 100644 NetworkingLab/app/src/main/res/values/colors.xml create mode 100644 NetworkingLab/app/src/main/res/values/dimens.xml create mode 100644 NetworkingLab/app/src/main/res/values/strings.xml create mode 100644 NetworkingLab/app/src/main/res/values/styles.xml create mode 100644 NetworkingLab/app/src/test/java/com/example/kylemcnee/networkinglab/ExampleUnitTest.java create mode 100644 NetworkingLab/build.gradle create mode 100644 NetworkingLab/gradle.properties create mode 100644 NetworkingLab/gradle/wrapper/gradle-wrapper.jar create mode 100644 NetworkingLab/gradle/wrapper/gradle-wrapper.properties create mode 100644 NetworkingLab/gradlew create mode 100644 NetworkingLab/gradlew.bat create mode 100644 NetworkingLab/settings.gradle diff --git a/NetworkingLab/.gitignore b/NetworkingLab/.gitignore new file mode 100644 index 0000000..c6cbe56 --- /dev/null +++ b/NetworkingLab/.gitignore @@ -0,0 +1,8 @@ +*.iml +.gradle +/local.properties +/.idea/workspace.xml +/.idea/libraries +.DS_Store +/build +/captures diff --git a/NetworkingLab/.idea/.name b/NetworkingLab/.idea/.name new file mode 100644 index 0000000..eea1ab1 --- /dev/null +++ b/NetworkingLab/.idea/.name @@ -0,0 +1 @@ +NetworkingLab \ No newline at end of file diff --git a/NetworkingLab/.idea/compiler.xml b/NetworkingLab/.idea/compiler.xml new file mode 100644 index 0000000..96cc43e --- /dev/null +++ b/NetworkingLab/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/NetworkingLab/.idea/copyright/profiles_settings.xml b/NetworkingLab/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/NetworkingLab/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/NetworkingLab/.idea/encodings.xml b/NetworkingLab/.idea/encodings.xml new file mode 100644 index 0000000..97626ba --- /dev/null +++ b/NetworkingLab/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/NetworkingLab/.idea/gradle.xml b/NetworkingLab/.idea/gradle.xml new file mode 100644 index 0000000..fc132b2 --- /dev/null +++ b/NetworkingLab/.idea/gradle.xml @@ -0,0 +1,19 @@ + + + + + + \ No newline at end of file diff --git a/NetworkingLab/.idea/misc.xml b/NetworkingLab/.idea/misc.xml new file mode 100644 index 0000000..cca2cda --- /dev/null +++ b/NetworkingLab/.idea/misc.xml @@ -0,0 +1,62 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1.8 + + + + + + + + \ No newline at end of file diff --git a/NetworkingLab/.idea/modules.xml b/NetworkingLab/.idea/modules.xml new file mode 100644 index 0000000..c3c8048 --- /dev/null +++ b/NetworkingLab/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/NetworkingLab/.idea/runConfigurations.xml b/NetworkingLab/.idea/runConfigurations.xml new file mode 100644 index 0000000..7f68460 --- /dev/null +++ b/NetworkingLab/.idea/runConfigurations.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/NetworkingLab/.idea/vcs.xml b/NetworkingLab/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/NetworkingLab/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/NetworkingLab/app/.gitignore b/NetworkingLab/app/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/NetworkingLab/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/NetworkingLab/app/build.gradle b/NetworkingLab/app/build.gradle new file mode 100644 index 0000000..7c64040 --- /dev/null +++ b/NetworkingLab/app/build.gradle @@ -0,0 +1,26 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 23 + buildToolsVersion "23.0.2" + + defaultConfig { + applicationId "com.example.kylemcnee.networkinglab" + minSdkVersion 16 + targetSdkVersion 23 + versionCode 1 + versionName "1.0" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + compile fileTree(dir: 'libs', include: ['*.jar']) + testCompile 'junit:junit:4.12' + compile 'com.android.support:appcompat-v7:23.1.1' +} diff --git a/NetworkingLab/app/proguard-rules.pro b/NetworkingLab/app/proguard-rules.pro new file mode 100644 index 0000000..8a2f402 --- /dev/null +++ b/NetworkingLab/app/proguard-rules.pro @@ -0,0 +1,17 @@ +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in C:\Android\sdk/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} diff --git a/NetworkingLab/app/src/androidTest/java/com/example/kylemcnee/networkinglab/ApplicationTest.java b/NetworkingLab/app/src/androidTest/java/com/example/kylemcnee/networkinglab/ApplicationTest.java new file mode 100644 index 0000000..3e760bf --- /dev/null +++ b/NetworkingLab/app/src/androidTest/java/com/example/kylemcnee/networkinglab/ApplicationTest.java @@ -0,0 +1,13 @@ +package com.example.kylemcnee.networkinglab; + +import android.app.Application; +import android.test.ApplicationTestCase; + +/** + * Testing Fundamentals + */ +public class ApplicationTest extends ApplicationTestCase { + public ApplicationTest() { + super(Application.class); + } +} \ No newline at end of file diff --git a/NetworkingLab/app/src/main/AndroidManifest.xml b/NetworkingLab/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..2f3e1e9 --- /dev/null +++ b/NetworkingLab/app/src/main/AndroidManifest.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + diff --git a/NetworkingLab/app/src/main/java/com/example/kylemcnee/networkinglab/MainActivity.java b/NetworkingLab/app/src/main/java/com/example/kylemcnee/networkinglab/MainActivity.java new file mode 100644 index 0000000..a6b0592 --- /dev/null +++ b/NetworkingLab/app/src/main/java/com/example/kylemcnee/networkinglab/MainActivity.java @@ -0,0 +1,154 @@ +package com.example.kylemcnee.networkinglab; + +import android.os.AsyncTask; +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.view.View; +import android.widget.ArrayAdapter; +import android.widget.Button; +import android.widget.ListView; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +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.ProtocolException; +import java.net.URL; +import java.util.ArrayList; + +public class MainActivity extends AppCompatActivity { + Button mCerealButton; + Button mChocolateButton; + Button mTeaButton; + ListView mListView; + + ArrayList mProductDisplayArray = new ArrayList(); + ArrayAdapter mAdapter; + + String mCerealQuery = "http://api.walmartlabs.com/v1/search?query=cereal&format=json&apiKey=xx2c73n6acnbcdhjqwk3gp8n"; + String mChocolateQuery = "http://api.walmartlabs.com/v1/search?query=chocolate&format=json&apiKey=xx2c73n6acnbcdhjqwk3gp8n"; + String mTeaQuery = "http://api.walmartlabs.com/v1/search?query=tea&format=json&apiKey=xx2c73n6acnbcdhjqwk3gp8n"; + + SearchAsyncTask mCerealTask; + SearchAsyncTask mChocolatetask; + SearchAsyncTask mTeaTask; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + mCerealButton = (Button) findViewById(R.id.cerealButton); + mChocolateButton = (Button) findViewById(R.id.chocolateButton); + mTeaButton = (Button) findViewById(R.id.teaButton); + mListView = (ListView)findViewById(R.id.listView); + + mAdapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, mProductDisplayArray); + + + mCerealButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (mCerealTask != null && mCerealTask.getStatus() != SearchAsyncTask.Status.FINISHED) { + mCerealTask.cancel(true); + } + mCerealTask = new SearchAsyncTask(); + mCerealTask.execute(mCerealQuery); + } + }); + + mChocolateButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (mChocolatetask != null && mChocolatetask.getStatus() != SearchAsyncTask.Status.FINISHED){ + mChocolatetask.cancel(true); + } + mChocolatetask = new SearchAsyncTask(); + mChocolatetask.execute(mChocolateQuery); + } + }); + + mTeaButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (mTeaTask != null && mTeaTask.getStatus() != SearchAsyncTask.Status.FINISHED){ + mTeaTask.cancel(true); + } + mTeaTask = new SearchAsyncTask(); + mTeaTask.execute(mTeaQuery); + } + }); + + mListView.setAdapter(mAdapter); + + } + + public class SearchAsyncTask extends AsyncTask{ + + @Override + protected Void doInBackground(String... params) { + String product = ""; + try { + URL url = new URL(params[0]); + HttpURLConnection connection = (HttpURLConnection)url.openConnection(); + connection.setRequestMethod("GET"); + + connection.connect(); + InputStream inputStream = connection.getInputStream(); + product = getInputData(inputStream); + + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (ProtocolException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + try { + JSONObject object = new JSONObject(product); +// String status = object.getString("stat"); + + JSONArray productArray = object.getJSONArray("items"); + mProductDisplayArray.clear(); + for (int i = 0; i < productArray.length(); i++){ + JSONObject secondObject = productArray.optJSONObject(i); + String name = secondObject.optString("name"); + + mProductDisplayArray.add(name); + } + + + } catch (JSONException e) { + e.printStackTrace(); + } + + + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + super.onPostExecute(aVoid); + mAdapter.notifyDataSetChanged(); + } + } + private String getInputData(InputStream inStream) throws IOException { + StringBuilder builder = new StringBuilder(); + BufferedReader reader = new BufferedReader(new InputStreamReader(inStream)); + + String product; + + while ((product = reader.readLine()) != null){ + builder.append(product); + } + + return builder.toString(); + } +} diff --git a/NetworkingLab/app/src/main/res/layout/activity_main.xml b/NetworkingLab/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..b1180aa --- /dev/null +++ b/NetworkingLab/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,34 @@ + + + +