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..fe2b322 --- /dev/null +++ b/MyApplication/app/src/main/java/com/example/android/walmart/MainActivity.java @@ -0,0 +1,158 @@ +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; + + WalmartAsyncTask mWalmartAsyncTask; + + @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) { + if (mWalmartAsyncTask != null && mWalmartAsyncTask.getStatus() != AsyncTask.Status.FINISHED) { + mWalmartAsyncTask.cancel(true); + } + mWalmartAsyncTask = new WalmartAsyncTask(); + mWalmartAsyncTask.execute(mCerealString); + + } + }); + + mChocolateButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (mWalmartAsyncTask != null && mWalmartAsyncTask.getStatus() != AsyncTask.Status.FINISHED) { + mWalmartAsyncTask.cancel(true); + } + mWalmartAsyncTask = new WalmartAsyncTask(); + + mWalmartAsyncTask.execute(mChocolateString); + } + }); + + mTeaButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (mWalmartAsyncTask != null && mWalmartAsyncTask.getStatus() != AsyncTask.Status.FINISHED) { + mWalmartAsyncTask.cancel(true); + } + mWalmartAsyncTask = new WalmartAsyncTask(); + + mWalmartAsyncTask.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); +// if (isCancelled()){break;} + } + + + } 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 @@ + + + + + + + +