diff --git a/NetworkingAndroid/.gitignore b/NetworkingAndroid/.gitignore new file mode 100644 index 0000000..c6cbe56 --- /dev/null +++ b/NetworkingAndroid/.gitignore @@ -0,0 +1,8 @@ +*.iml +.gradle +/local.properties +/.idea/workspace.xml +/.idea/libraries +.DS_Store +/build +/captures diff --git a/NetworkingAndroid/.idea/.name b/NetworkingAndroid/.idea/.name new file mode 100644 index 0000000..006c6ac --- /dev/null +++ b/NetworkingAndroid/.idea/.name @@ -0,0 +1 @@ +NetworkingAndroid \ No newline at end of file diff --git a/NetworkingAndroid/.idea/compiler.xml b/NetworkingAndroid/.idea/compiler.xml new file mode 100644 index 0000000..96cc43e --- /dev/null +++ b/NetworkingAndroid/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/NetworkingAndroid/.idea/copyright/profiles_settings.xml b/NetworkingAndroid/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/NetworkingAndroid/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/NetworkingAndroid/.idea/gradle.xml b/NetworkingAndroid/.idea/gradle.xml new file mode 100644 index 0000000..1bbc21d --- /dev/null +++ b/NetworkingAndroid/.idea/gradle.xml @@ -0,0 +1,18 @@ + + + + + + \ No newline at end of file diff --git a/NetworkingAndroid/.idea/misc.xml b/NetworkingAndroid/.idea/misc.xml new file mode 100644 index 0000000..000e28a --- /dev/null +++ b/NetworkingAndroid/.idea/misc.xml @@ -0,0 +1,96 @@ + + + + + + + + + + + + + + + + + + Android + + + Android Lint + + + CorrectnessLintAndroid + + + General + + + LintAndroid + + + Maven + + + + + Android + + + + + + + + + + + + + + + + + + + + + + + + + 1.7 + + + + + + + + \ No newline at end of file diff --git a/NetworkingAndroid/.idea/modules.xml b/NetworkingAndroid/.idea/modules.xml new file mode 100644 index 0000000..45437b7 --- /dev/null +++ b/NetworkingAndroid/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/NetworkingAndroid/.idea/runConfigurations.xml b/NetworkingAndroid/.idea/runConfigurations.xml new file mode 100644 index 0000000..7f68460 --- /dev/null +++ b/NetworkingAndroid/.idea/runConfigurations.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/NetworkingAndroid/.idea/vcs.xml b/NetworkingAndroid/.idea/vcs.xml new file mode 100644 index 0000000..6564d52 --- /dev/null +++ b/NetworkingAndroid/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/NetworkingAndroid/app/.gitignore b/NetworkingAndroid/app/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/NetworkingAndroid/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/NetworkingAndroid/app/build.gradle b/NetworkingAndroid/app/build.gradle new file mode 100644 index 0000000..6ae2c70 --- /dev/null +++ b/NetworkingAndroid/app/build.gradle @@ -0,0 +1,26 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 23 + buildToolsVersion "23.0.2" + + defaultConfig { + applicationId "com.adi.ho.jackie.networkingandroid" + 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/NetworkingAndroid/app/proguard-rules.pro b/NetworkingAndroid/app/proguard-rules.pro new file mode 100644 index 0000000..abda49d --- /dev/null +++ b/NetworkingAndroid/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/JHADI/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/NetworkingAndroid/app/src/androidTest/java/com/adi/ho/jackie/networkingandroid/ApplicationTest.java b/NetworkingAndroid/app/src/androidTest/java/com/adi/ho/jackie/networkingandroid/ApplicationTest.java new file mode 100644 index 0000000..ec7bbd3 --- /dev/null +++ b/NetworkingAndroid/app/src/androidTest/java/com/adi/ho/jackie/networkingandroid/ApplicationTest.java @@ -0,0 +1,13 @@ +package com.adi.ho.jackie.networkingandroid; + +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/NetworkingAndroid/app/src/main/AndroidManifest.xml b/NetworkingAndroid/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..98bcfad --- /dev/null +++ b/NetworkingAndroid/app/src/main/AndroidManifest.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + diff --git a/NetworkingAndroid/app/src/main/java/com/adi/ho/jackie/networkingandroid/MainActivity.java b/NetworkingAndroid/app/src/main/java/com/adi/ho/jackie/networkingandroid/MainActivity.java new file mode 100644 index 0000000..199a295 --- /dev/null +++ b/NetworkingAndroid/app/src/main/java/com/adi/ho/jackie/networkingandroid/MainActivity.java @@ -0,0 +1,220 @@ +package com.adi.ho.jackie.networkingandroid; + +import android.os.AsyncTask; +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.view.View; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; +import android.widget.Button; +import android.widget.ListView; +import android.widget.ProgressBar; +import android.widget.Spinner; +import android.widget.TextView; + +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; +import java.util.HashMap; + +public class MainActivity extends AppCompatActivity { + + private String walmartKey = "5hbnkvrdvq3dafvfax34meez"; + String url = "http://api.walmartlabs.com/v1/search?query="; + private String query; + //private ArrayList jsonItemNames; + private ArrayList jsonItemList; + //private ArrayAdapter listAdapter; + private WalmartItemAdapter listAdapter; + DownloadCategoryAsyncTask mDownloadCategoryAsyncTask; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + Spinner categorySpinner = (Spinner) findViewById(R.id.category_spinner); + ListView walmartList = (ListView) findViewById(R.id.list_listview); + Button teaSelect = (Button) findViewById(R.id.tea_but); + Button chocSelect = (Button) findViewById(R.id.choc_but); + Button cerealSelect = (Button) findViewById(R.id.cereal_but); + + mDownloadCategoryAsyncTask = new DownloadCategoryAsyncTask(); + + ArrayList spinnerList = new ArrayList<>(); + jsonItemList = new ArrayList<>(); + spinnerList.add("Select an item"); + spinnerList.add("Tea"); + spinnerList.add("Cereal"); + spinnerList.add("Chocolate"); + + ArrayAdapter spinnerAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, spinnerList); + spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + categorySpinner.setAdapter(spinnerAdapter); + categorySpinner.setOnItemSelectedListener(spinListener); + + + // listAdapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1 , jsonItemList); + listAdapter = new WalmartItemAdapter(MainActivity.this, jsonItemList); + walmartList.setAdapter(listAdapter); + + //button listeners + teaSelect.setOnClickListener(teaListener); + chocSelect.setOnClickListener(chocListener); + cerealSelect.setOnClickListener(cerealListener); + + } + + AdapterView.OnItemSelectedListener spinListener = new AdapterView.OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView parent, View view, int position, long id) { + switch (position) { + case 0: + return; + case 1: + query = "Tea"; + break; + case 2: + query = "Cereal"; + break; + case 3: + query = "Chocolate"; + break; + default: + } + DownloadCategoryAsyncTask downloadCategoryAsyncTask = new DownloadCategoryAsyncTask(); + + downloadCategoryAsyncTask.execute(url + query + "&format=json&apiKey=" + walmartKey); + } + + @Override + public void onNothingSelected(AdapterView parent) { + + } + }; + + View.OnClickListener teaListener = new View.OnClickListener() { + @Override + public void onClick(View v) { + String query = "Tea"; + if (mDownloadCategoryAsyncTask.getStatus() != null && mDownloadCategoryAsyncTask.getStatus() != AsyncTask.Status.FINISHED) { + mDownloadCategoryAsyncTask.cancel(true); + mDownloadCategoryAsyncTask = new DownloadCategoryAsyncTask(); + mDownloadCategoryAsyncTask.execute(url + query + "&format=json&apiKey=" + walmartKey); + } else { + mDownloadCategoryAsyncTask = new DownloadCategoryAsyncTask(); + mDownloadCategoryAsyncTask.execute(url + query + "&format=json&apiKey=" + walmartKey); + } + } + }; + View.OnClickListener chocListener = new View.OnClickListener() { + @Override + public void onClick(View v) { + String query = "Chocolate"; + if (mDownloadCategoryAsyncTask.getStatus() != null && mDownloadCategoryAsyncTask.getStatus() != AsyncTask.Status.FINISHED) { + mDownloadCategoryAsyncTask.cancel(true); + mDownloadCategoryAsyncTask = new DownloadCategoryAsyncTask(); + mDownloadCategoryAsyncTask.execute(url + query + "&format=json&apiKey=" + walmartKey); + } else { + mDownloadCategoryAsyncTask = new DownloadCategoryAsyncTask(); + mDownloadCategoryAsyncTask.execute(url + query + "&format=json&apiKey=" + walmartKey); + } + } + }; + View.OnClickListener cerealListener = new View.OnClickListener() { + @Override + public void onClick(View v) { + String query = "Cereal"; + if (mDownloadCategoryAsyncTask.getStatus() != null && mDownloadCategoryAsyncTask.getStatus() != AsyncTask.Status.FINISHED) { + mDownloadCategoryAsyncTask.cancel(true); + mDownloadCategoryAsyncTask = new DownloadCategoryAsyncTask(); + mDownloadCategoryAsyncTask.execute(url + query + "&format=json&apiKey=" + walmartKey); + } else { + mDownloadCategoryAsyncTask = new DownloadCategoryAsyncTask(); + mDownloadCategoryAsyncTask.execute(url + query + "&format=json&apiKey=" + walmartKey); + } + } + }; + + private String getInputData(InputStream inStream) throws IOException { + + StringBuilder builder = new StringBuilder(); + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inStream)); // + + String data; //usually for xml or json, where you can parse it + + while ((data = bufferedReader.readLine()) != null) { + builder.append(data);//add each line on the reader to the data + } + bufferedReader.close(); + return builder.toString(); + + } + + private class DownloadCategoryAsyncTask extends AsyncTask { + String data; + + ProgressBar progressBar = (ProgressBar) findViewById(R.id.progress_bar); + + @Override + protected void onPreExecute() { + super.onPreExecute(); + + + progressBar.setVisibility(View.VISIBLE); + + } + + @Override + protected String doInBackground(String... params) { + try { + URL url = new URL(params[0]); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.connect(); // talking to server at this point + + InputStream inStream = connection.getInputStream(); //Gets data from internet + + data = getInputData(inStream); + + + } catch (Throwable tho) { + tho.printStackTrace(); + } + + + try { + JSONObject parentObject = new JSONObject(data); + JSONArray itemsArrayJsonObject = parentObject.optJSONArray("items"); + + jsonItemList.clear(); + for (int i = 0; i < itemsArrayJsonObject.length(); i++) { + JSONObject actualyItemJsonObject = itemsArrayJsonObject.getJSONObject(i); + String name = actualyItemJsonObject.getString("name"); + String price = actualyItemJsonObject.getString("salePrice"); + jsonItemList.add(new WalmartItem(name, price)); + } + + } catch (JSONException e) { + e.printStackTrace(); + } + + return data; + } + + @Override + protected void onPostExecute(String s) { + super.onPostExecute(s); + progressBar.setVisibility(View.GONE); + // listAdapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1,jsonItemNames); + listAdapter.notifyDataSetChanged(); + } + } +} diff --git a/NetworkingAndroid/app/src/main/java/com/adi/ho/jackie/networkingandroid/WalmartItem.java b/NetworkingAndroid/app/src/main/java/com/adi/ho/jackie/networkingandroid/WalmartItem.java new file mode 100644 index 0000000..5f360f0 --- /dev/null +++ b/NetworkingAndroid/app/src/main/java/com/adi/ho/jackie/networkingandroid/WalmartItem.java @@ -0,0 +1,22 @@ +package com.adi.ho.jackie.networkingandroid; + +/** + * Created by JHADI on 2/24/16. + */ +public class WalmartItem { + private String itemName; + private String itemPrice; + + public WalmartItem(String itemName, String itemPrice){ + this.itemName = itemName; + this.itemPrice = itemPrice; + + } + public String getItemPrice() { + return itemPrice; + } + + public String getItemName() { + return itemName; + } +} diff --git a/NetworkingAndroid/app/src/main/java/com/adi/ho/jackie/networkingandroid/WalmartItemAdapter.java b/NetworkingAndroid/app/src/main/java/com/adi/ho/jackie/networkingandroid/WalmartItemAdapter.java new file mode 100644 index 0000000..4ae643b --- /dev/null +++ b/NetworkingAndroid/app/src/main/java/com/adi/ho/jackie/networkingandroid/WalmartItemAdapter.java @@ -0,0 +1,45 @@ +package com.adi.ho.jackie.networkingandroid; + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.TextView; + +import org.w3c.dom.Text; + +import java.util.ArrayList; + +/** + * Created by JHADI on 2/24/16. + */ +public class WalmartItemAdapter extends ArrayAdapter { + private ArrayList walmartList; + private Context context; + public WalmartItemAdapter(Context context, ArrayList walmartList) { + super(context, -1); + this.context = context; + this.walmartList = walmartList; + + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + View v = layoutInflater.inflate(R.layout.custom_list_layout, parent, false); + + TextView itemNameText = (TextView)v.findViewById(R.id.item_name); + TextView itemPriceText = (TextView)v.findViewById(R.id.item_price); + + itemNameText.setText(walmartList.get(position).getItemName()); + itemPriceText.setText("$"+walmartList.get(position).getItemPrice()); + + return v; + } + + @Override + public int getCount() { + return walmartList.size(); + } +} diff --git a/NetworkingAndroid/app/src/main/res/layout/activity_main.xml b/NetworkingAndroid/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..d79e2ca --- /dev/null +++ b/NetworkingAndroid/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,67 @@ + + + + + +