From 17863c737f494af46d38ae6b2ee03198ed155913 Mon Sep 17 00:00:00 2001 From: WasabiMayo Date: Thu, 25 Feb 2016 09:57:31 -0500 Subject: [PATCH 1/2] Claire & Will L - Networking in android lab, bonus completed --- MyApplication/.gitignore | 8 + MyApplication/.idea/.name | 1 + MyApplication/.idea/compiler.xml | 22 +++ .../.idea/copyright/profiles_settings.xml | 3 + MyApplication/.idea/encodings.xml | 6 + MyApplication/.idea/gradle.xml | 18 ++ MyApplication/.idea/misc.xml | 62 +++++++ 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 ++ .../wasabi/myapplication/ApplicationTest.java | 13 ++ .../app/src/main/AndroidManifest.xml | 23 +++ .../wasabi/myapplication/MainActivity.java | 172 ++++++++++++++++++ .../app/src/main/res/layout/activity_main.xml | 39 ++++ .../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 ++ .../wasabi/myapplication/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/encodings.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/wasabi/myapplication/ApplicationTest.java create mode 100644 MyApplication/app/src/main/AndroidManifest.xml create mode 100644 MyApplication/app/src/main/java/com/example/wasabi/myapplication/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/wasabi/myapplication/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 100644 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..b3405b3 --- /dev/null +++ b/MyApplication/.idea/.name @@ -0,0 +1 @@ +My Application \ 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/encodings.xml b/MyApplication/.idea/encodings.xml new file mode 100644 index 0000000..97626ba --- /dev/null +++ b/MyApplication/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/MyApplication/.idea/gradle.xml b/MyApplication/.idea/gradle.xml new file mode 100644 index 0000000..8d2df47 --- /dev/null +++ b/MyApplication/.idea/gradle.xml @@ -0,0 +1,18 @@ + + + + + + \ No newline at end of file diff --git a/MyApplication/.idea/misc.xml b/MyApplication/.idea/misc.xml new file mode 100644 index 0000000..7158618 --- /dev/null +++ b/MyApplication/.idea/misc.xml @@ -0,0 +1,62 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1.8 + + + + + + + + \ 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..2e4d121 --- /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.wasabi.myapplication" + minSdkVersion 14 + 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..7fe377b --- /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 C:\Users\Wasabi\AppData\Local\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/wasabi/myapplication/ApplicationTest.java b/MyApplication/app/src/androidTest/java/com/example/wasabi/myapplication/ApplicationTest.java new file mode 100644 index 0000000..9cfbf00 --- /dev/null +++ b/MyApplication/app/src/androidTest/java/com/example/wasabi/myapplication/ApplicationTest.java @@ -0,0 +1,13 @@ +package com.example.wasabi.myapplication; + +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..241d86b --- /dev/null +++ b/MyApplication/app/src/main/AndroidManifest.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + diff --git a/MyApplication/app/src/main/java/com/example/wasabi/myapplication/MainActivity.java b/MyApplication/app/src/main/java/com/example/wasabi/myapplication/MainActivity.java new file mode 100644 index 0000000..75b9c9e --- /dev/null +++ b/MyApplication/app/src/main/java/com/example/wasabi/myapplication/MainActivity.java @@ -0,0 +1,172 @@ +package com.example.wasabi.myapplication; + +import android.content.Context; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; +import android.os.AsyncTask; +import android.support.annotation.MainThread; +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 android.widget.Toast; + +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.URL; +import java.util.ArrayList; +import java.util.List; + +public class MainActivity extends AppCompatActivity { + + Button cerealButton, chocolateButton, teaButton; + ListView mListView; + ArrayAdapter mAdapter; + ArrayList mArrayList; + String query, url; + WalmartAsyncTask mTask; + + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + url = "http://api.walmartlabs.com/v1/search?apiKey=ypbx38jaxteyq7kky7mskam5&query="; + + cerealButton = (Button)findViewById(R.id.cerealButton); + chocolateButton = (Button)findViewById(R.id.chocolateButton); + teaButton = (Button)findViewById(R.id.teaButton); + mListView = (ListView)findViewById(R.id.listview); + + mArrayList = new ArrayList(); + mAdapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,mArrayList); + mListView.setAdapter(mAdapter); + + + cerealButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); + NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); + if (networkInfo != null && networkInfo.isConnected()) { + if (mTask != null && (mTask.getStatus() != AsyncTask.Status.FINISHED)) { + mTask.cancel(true); + } + mTask = new WalmartAsyncTask(); + query = "cereal"; + mTask.execute(url+query); + } else { + Toast.makeText(MainActivity.this, "Please connect to internet", Toast.LENGTH_SHORT).show(); + } + } + }); + + chocolateButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); + NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); + if (networkInfo != null && networkInfo.isConnected()) { + if (mTask != null && (mTask.getStatus() != AsyncTask.Status.FINISHED)) { + mTask.cancel(true); + } + mTask = new WalmartAsyncTask(); + query = "cereal"; + mTask.execute(url+query); + } else { + Toast.makeText(MainActivity.this, "Please connect to internet", Toast.LENGTH_SHORT).show(); + } + } + }); + + teaButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); + NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); + if (networkInfo != null && networkInfo.isConnected()) { + if (mTask != null && (mTask.getStatus() != AsyncTask.Status.FINISHED)) { + mTask.cancel(true); + } + mTask = new WalmartAsyncTask(); + query = "cereal"; + mTask.execute(url+query); + } else { + Toast.makeText(MainActivity.this, "Please connect to internet", Toast.LENGTH_SHORT).show(); + } + } + }); + + } + + public class WalmartAsyncTask extends AsyncTask{ + + @Override + protected String doInBackground(String... params) { + String data =""; + try { + URL url = new URL(params[0]); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.connect(); + InputStream inStream = connection.getInputStream(); + data = getInputData(inStream); + } catch (Throwable e) { + e.printStackTrace(); + } + + try { + JSONObject dataObject = new JSONObject(data); + JSONArray itemJsonArray = dataObject.getJSONArray("items"); + + mArrayList.clear(); + + for(int i = 0; i < itemJsonArray.length() ; i++){ + JSONObject object = itemJsonArray.getJSONObject(i); + String name = object.optString("name"); + double price = object.optDouble("salePrice"); + + mArrayList.add(name+", Price : "+price); + } + + } catch (JSONException e) { + e.printStackTrace(); + } + + return data; + } + + @Override + protected void onPostExecute(String s) { + super.onPostExecute(s); + mAdapter.notifyDataSetChanged(); + } + } + + private String getInputData(InputStream inStream) throws IOException{ + StringBuilder builder = new StringBuilder(); + BufferedReader reader = new BufferedReader(new InputStreamReader(inStream)); + + String data = null; + + while ((data = reader.readLine()) != null){ + builder.append(data); + } + + reader.close(); + + return builder.toString(); + } + + +} 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..9c1774d --- /dev/null +++ b/MyApplication/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,39 @@ + + + + +