From bb79b2c40d7d56c59597cdc604e92d9e0691eee6 Mon Sep 17 00:00:00 2001 From: chris-shum Date: Wed, 24 Feb 2016 17:44:13 -0500 Subject: [PATCH 1/2] Networking in Android Lab w/Bonus --- MyApplication/.gitignore | 8 + MyApplication/.idea/.name | 1 + MyApplication/.idea/compiler.xml | 22 +++ .../.idea/copyright/profiles_settings.xml | 3 + MyApplication/.idea/dictionaries/ShowMe.xml | 3 + MyApplication/.idea/gradle.xml | 19 +++ MyApplication/.idea/misc.xml | 77 +++++++++ MyApplication/.idea/modules.xml | 9 + MyApplication/.idea/runConfigurations.xml | 12 ++ MyApplication/.idea/vcs.xml | 6 + MyApplication/app/.gitignore | 1 + MyApplication/app/build.gradle | 26 +++ MyApplication/app/proguard-rules.pro | 17 ++ .../android/walmart/ApplicationTest.java | 13 ++ .../app/src/main/AndroidManifest.xml | 23 +++ .../example/android/walmart/MainActivity.java | 144 ++++++++++++++++ .../app/src/main/res/layout/activity_main.xml | 54 ++++++ .../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 ++ .../android/walmart/ExampleUnitTest.java | 15 ++ MyApplication/build.gradle | 23 +++ MyApplication/gradle.properties | 18 ++ .../gradle/wrapper/gradle-wrapper.jar | Bin 0 -> 53637 bytes .../gradle/wrapper/gradle-wrapper.properties | 6 + MyApplication/gradlew | 160 ++++++++++++++++++ MyApplication/gradlew.bat | 90 ++++++++++ MyApplication/settings.gradle | 1 + 35 files changed, 782 insertions(+) create mode 100644 MyApplication/.gitignore create mode 100644 MyApplication/.idea/.name create mode 100644 MyApplication/.idea/compiler.xml create mode 100644 MyApplication/.idea/copyright/profiles_settings.xml create mode 100644 MyApplication/.idea/dictionaries/ShowMe.xml create mode 100644 MyApplication/.idea/gradle.xml create mode 100644 MyApplication/.idea/misc.xml create mode 100644 MyApplication/.idea/modules.xml create mode 100644 MyApplication/.idea/runConfigurations.xml create mode 100644 MyApplication/.idea/vcs.xml create mode 100644 MyApplication/app/.gitignore create mode 100644 MyApplication/app/build.gradle create mode 100644 MyApplication/app/proguard-rules.pro create mode 100644 MyApplication/app/src/androidTest/java/com/example/android/walmart/ApplicationTest.java create mode 100644 MyApplication/app/src/main/AndroidManifest.xml create mode 100644 MyApplication/app/src/main/java/com/example/android/walmart/MainActivity.java create mode 100644 MyApplication/app/src/main/res/layout/activity_main.xml create mode 100644 MyApplication/app/src/main/res/mipmap-hdpi/ic_launcher.png create mode 100644 MyApplication/app/src/main/res/mipmap-mdpi/ic_launcher.png create mode 100644 MyApplication/app/src/main/res/mipmap-xhdpi/ic_launcher.png create mode 100644 MyApplication/app/src/main/res/mipmap-xxhdpi/ic_launcher.png create mode 100644 MyApplication/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png create mode 100644 MyApplication/app/src/main/res/values-w820dp/dimens.xml create mode 100644 MyApplication/app/src/main/res/values/colors.xml create mode 100644 MyApplication/app/src/main/res/values/dimens.xml create mode 100644 MyApplication/app/src/main/res/values/strings.xml create mode 100644 MyApplication/app/src/main/res/values/styles.xml create mode 100644 MyApplication/app/src/test/java/com/example/android/walmart/ExampleUnitTest.java create mode 100644 MyApplication/build.gradle create mode 100644 MyApplication/gradle.properties create mode 100644 MyApplication/gradle/wrapper/gradle-wrapper.jar create mode 100644 MyApplication/gradle/wrapper/gradle-wrapper.properties create mode 100755 MyApplication/gradlew create mode 100644 MyApplication/gradlew.bat create mode 100644 MyApplication/settings.gradle diff --git a/MyApplication/.gitignore b/MyApplication/.gitignore new file mode 100644 index 0000000..c6cbe56 --- /dev/null +++ b/MyApplication/.gitignore @@ -0,0 +1,8 @@ +*.iml +.gradle +/local.properties +/.idea/workspace.xml +/.idea/libraries +.DS_Store +/build +/captures diff --git a/MyApplication/.idea/.name b/MyApplication/.idea/.name new file mode 100644 index 0000000..0a6e8c4 --- /dev/null +++ b/MyApplication/.idea/.name @@ -0,0 +1 @@ +Walmart \ No newline at end of file diff --git a/MyApplication/.idea/compiler.xml b/MyApplication/.idea/compiler.xml new file mode 100644 index 0000000..96cc43e --- /dev/null +++ b/MyApplication/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/MyApplication/.idea/copyright/profiles_settings.xml b/MyApplication/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/MyApplication/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/MyApplication/.idea/dictionaries/ShowMe.xml b/MyApplication/.idea/dictionaries/ShowMe.xml new file mode 100644 index 0000000..02c1c32 --- /dev/null +++ b/MyApplication/.idea/dictionaries/ShowMe.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/MyApplication/.idea/gradle.xml b/MyApplication/.idea/gradle.xml new file mode 100644 index 0000000..39139a6 --- /dev/null +++ b/MyApplication/.idea/gradle.xml @@ -0,0 +1,19 @@ + + + + + + \ No newline at end of file diff --git a/MyApplication/.idea/misc.xml b/MyApplication/.idea/misc.xml new file mode 100644 index 0000000..04c63e4 --- /dev/null +++ b/MyApplication/.idea/misc.xml @@ -0,0 +1,77 @@ + + + + + + + + + + + + + + + + + + + + Android + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/MyApplication/.idea/modules.xml b/MyApplication/.idea/modules.xml new file mode 100644 index 0000000..54b7b3f --- /dev/null +++ b/MyApplication/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/MyApplication/.idea/runConfigurations.xml b/MyApplication/.idea/runConfigurations.xml new file mode 100644 index 0000000..7f68460 --- /dev/null +++ b/MyApplication/.idea/runConfigurations.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/MyApplication/.idea/vcs.xml b/MyApplication/.idea/vcs.xml new file mode 100644 index 0000000..6564d52 --- /dev/null +++ b/MyApplication/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/MyApplication/app/.gitignore b/MyApplication/app/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/MyApplication/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/MyApplication/app/build.gradle b/MyApplication/app/build.gradle new file mode 100644 index 0000000..2ae271a --- /dev/null +++ b/MyApplication/app/build.gradle @@ -0,0 +1,26 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 23 + buildToolsVersion "23.0.2" + + defaultConfig { + applicationId "com.example.android.walmart" + 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/MyApplication/app/proguard-rules.pro b/MyApplication/app/proguard-rules.pro new file mode 100644 index 0000000..ba26f6d --- /dev/null +++ b/MyApplication/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 /Users/ShowMe/Library/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/MyApplication/app/src/androidTest/java/com/example/android/walmart/ApplicationTest.java b/MyApplication/app/src/androidTest/java/com/example/android/walmart/ApplicationTest.java new file mode 100644 index 0000000..fd1a2fa --- /dev/null +++ b/MyApplication/app/src/androidTest/java/com/example/android/walmart/ApplicationTest.java @@ -0,0 +1,13 @@ +package com.example.android.walmart; + +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/MyApplication/app/src/main/AndroidManifest.xml b/MyApplication/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..bae85a4 --- /dev/null +++ b/MyApplication/app/src/main/AndroidManifest.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + diff --git a/MyApplication/app/src/main/java/com/example/android/walmart/MainActivity.java b/MyApplication/app/src/main/java/com/example/android/walmart/MainActivity.java new file mode 100644 index 0000000..eac4d01 --- /dev/null +++ b/MyApplication/app/src/main/java/com/example/android/walmart/MainActivity.java @@ -0,0 +1,144 @@ +package com.example.android.walmart; + +import android.os.AsyncTask; +import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; +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.URL; +import java.util.ArrayList; + +public class MainActivity extends AppCompatActivity { + + Button mCerealButton; + Button mChocolateButton; + Button mTeaButton; + ListView mListResults; + String mCerealString; + String mChocolateString; + String mTeaString; + ArrayList mListArray; + ArrayAdapter mAdapter; + + @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); + mListResults = (ListView) findViewById(R.id.theListView); + mCerealString = "http://api.walmartlabs.com/v1/search?query=cereal&format=json&apiKey=rbgd6kgpdyx3xzpjjvtgn9t4"; + mChocolateString = "http://api.walmartlabs.com/v1/search?query=chocolate&format=json&apiKey=rbgd6kgpdyx3xzpjjvtgn9t4"; + mTeaString = "http://api.walmartlabs.com/v1/search?query=tea&format=json&apiKey=rbgd6kgpdyx3xzpjjvtgn9t4"; + mListArray = new ArrayList<>(); + mAdapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, mListArray); + mListResults.setAdapter(mAdapter); + + mCerealButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + WalmartAsyncTask walmartAsyncTask = new WalmartAsyncTask(); + walmartAsyncTask.execute(mCerealString); + } + }); + + mChocolateButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + WalmartAsyncTask walmartAsyncTask = new WalmartAsyncTask(); + walmartAsyncTask.execute(mChocolateString); + } + }); + + mTeaButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + WalmartAsyncTask walmartAsyncTask = new WalmartAsyncTask(); + walmartAsyncTask.execute(mTeaString); + + } + }); + } + + private String getInputData(InputStream inStream) throws IOException { + StringBuilder builder = new StringBuilder(); + BufferedReader reader = new BufferedReader(new InputStreamReader(inStream)); + + String data; + + while ((data = reader.readLine()) != null) { + builder.append(data); + } + + reader.close(); + + + return builder.toString(); + + } + + public class WalmartAsyncTask extends AsyncTask { + + + @Override + protected String doInBackground(String... urls) { + String data = ""; + + try { + URL url = new URL(urls[0]); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.connect(); + + InputStream inStream = connection.getInputStream(); + + data = getInputData(inStream); + } catch (Throwable throwable) { + throwable.printStackTrace(); + ; + } + + try { + JSONObject dataObject = new JSONObject(data); + JSONArray itemsArrays = dataObject.getJSONArray("items"); + + mListArray.clear(); + for (int i = 0; i < itemsArrays.length(); i++) { + JSONObject object = itemsArrays.optJSONObject(i); + String name = object.optString("name"); + String price = object.optString("salePrice"); + String nameAndPrice = name + "\nPrice: $" + price; + mListArray.add(nameAndPrice); + } + + + } catch (JSONException e) { + e.printStackTrace(); + } + + return null; + } + + @Override + protected void onPostExecute(String s) { + super.onPostExecute(s); + + mAdapter.notifyDataSetChanged(); + } + } + + +} diff --git a/MyApplication/app/src/main/res/layout/activity_main.xml b/MyApplication/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..54d24ca --- /dev/null +++ b/MyApplication/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,54 @@ + + + + + + + +