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/gradle.xml b/NetworkingLab/.idea/gradle.xml new file mode 100644 index 0000000..8d2df47 --- /dev/null +++ b/NetworkingLab/.idea/gradle.xml @@ -0,0 +1,18 @@ + + + + + + \ No newline at end of file diff --git a/NetworkingLab/.idea/misc.xml b/NetworkingLab/.idea/misc.xml new file mode 100644 index 0000000..7158618 --- /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..6564d52 --- /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..85e1dec --- /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.ra.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..028e98d --- /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 /Users/Ra/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/NetworkingLab/app/src/androidTest/java/com/example/ra/networkinglab/ApplicationTest.java b/NetworkingLab/app/src/androidTest/java/com/example/ra/networkinglab/ApplicationTest.java new file mode 100644 index 0000000..4907c71 --- /dev/null +++ b/NetworkingLab/app/src/androidTest/java/com/example/ra/networkinglab/ApplicationTest.java @@ -0,0 +1,13 @@ +package com.example.ra.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..ad9eeba --- /dev/null +++ b/NetworkingLab/app/src/main/AndroidManifest.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + diff --git a/NetworkingLab/app/src/main/java/com/example/ra/networkinglab/MainActivity.java b/NetworkingLab/app/src/main/java/com/example/ra/networkinglab/MainActivity.java new file mode 100644 index 0000000..854610a --- /dev/null +++ b/NetworkingLab/app/src/main/java/com/example/ra/networkinglab/MainActivity.java @@ -0,0 +1,152 @@ +package com.example.ra.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.BufferedInputStream; +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; +import java.util.Scanner; + +public class MainActivity extends AppCompatActivity { + Button mCereal; + Button mTea; + Button mChocolate; + private WalmartAsyncTask mWalmartAsync; + ListView mListview; + ArrayList mArrayList; + ArrayAdapter mAdapter; + String urlTea= "http://api.walmartlabs.com/v1/search?query=tea&format=json&apiKey=jcpk6chshjwn5nbq2khnrvm9"; + String urlCereal="http://api.walmartlabs.com/v1/search?query=cereal&format=json&apiKey=jcpk6chshjwn5nbq2khnrvm9"; + String urlChoc=" http://api.walmartlabs.com/v1/search?query=chocolate&format=json&apiKey=jcpk6chshjwn5nbq2khnrvm9"; + + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + mArrayList= new ArrayList(); + mListview=(ListView)findViewById(R.id.listView); + mAdapter= new ArrayAdapter(this,android.R.layout.simple_list_item_1,mArrayList); + mListview.setAdapter(mAdapter); + mWalmartAsync= new WalmartAsyncTask(); + + mTea=(Button)findViewById(R.id.tea); + mCereal=(Button)findViewById(R.id.cereal); + mChocolate=(Button)findViewById(R.id.choc); + + mTea.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if(mWalmartAsync.getStatus()==AsyncTask.Status.RUNNING){ + mWalmartAsync.cancel(true); + } + mWalmartAsync= new WalmartAsyncTask(); + mWalmartAsync.execute(urlTea); + } + }); + + mCereal.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if(mWalmartAsync.getStatus()==AsyncTask.Status.RUNNING){ + mWalmartAsync.cancel(true); + } + WalmartAsyncTask walmartAsyncTask= new WalmartAsyncTask(); + walmartAsyncTask.execute(urlCereal); + } + }); + + mChocolate.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if(mWalmartAsync.getStatus()==AsyncTask.Status.RUNNING){ + mWalmartAsync.cancel(true); + } + WalmartAsyncTask walmartAsyncTask= new WalmartAsyncTask(); + walmartAsyncTask.execute(urlChoc); + } + }); + + } + + private String getInputData(InputStream inputStream) throws IOException { + StringBuilder stringBuilder= new StringBuilder(); + BufferedReader bufferedReader= new BufferedReader(new InputStreamReader(inputStream)); + String data; + + while ((data=bufferedReader.readLine()) !=null){ + stringBuilder.append(data); + } + bufferedReader.close(); + + return stringBuilder.toString(); + } + public class WalmartAsyncTask extends AsyncTask{ + String data= " "; + + @Override + protected String doInBackground(String... urls) { + + try { + URL url = new URL(urls[0]); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.connect(); + + InputStream inputStream = connection.getInputStream(); + data = getInputData(inputStream); + + + } catch (Throwable thr) { + thr.fillInStackTrace(); + + + } + try { + + JSONObject dataObject = new JSONObject(data); + + JSONArray teaArray = dataObject.getJSONArray("items"); + + mArrayList.clear(); + + for (int i = 0; i < teaArray.length(); i++) { + JSONObject object = teaArray.optJSONObject(i); + String title = object.optString("name"); + + mArrayList.add(title); + } + int i = 0;i++; + + } catch (JSONException e) { + + e.printStackTrace(); + } + return data; + } + + @Override + protected void onPostExecute(String s) { + super.onPostExecute(s); + mAdapter.notifyDataSetChanged(); + } + } + +} 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..c2d49c3 --- /dev/null +++ b/NetworkingLab/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,42 @@ + + + +