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/encodings.xml b/NetworkingLab/.idea/encodings.xml new file mode 100644 index 0000000..97626ba --- /dev/null +++ b/NetworkingLab/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/NetworkingLab/.idea/gradle.xml b/NetworkingLab/.idea/gradle.xml new file mode 100644 index 0000000..d390b65 --- /dev/null +++ b/NetworkingLab/.idea/gradle.xml @@ -0,0 +1,19 @@ + + + + + + \ No newline at end of file diff --git a/NetworkingLab/.idea/misc.xml b/NetworkingLab/.idea/misc.xml new file mode 100644 index 0000000..c60d22d --- /dev/null +++ b/NetworkingLab/.idea/misc.xml @@ -0,0 +1,62 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1.7 + + + + + + + + \ 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..bf78d18 --- /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.todo.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..0516634 --- /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 C:\Users\Todo\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/NetworkingLab/app/src/androidTest/java/com/example/todo/networkinglab/ApplicationTest.java b/NetworkingLab/app/src/androidTest/java/com/example/todo/networkinglab/ApplicationTest.java new file mode 100644 index 0000000..1974119 --- /dev/null +++ b/NetworkingLab/app/src/androidTest/java/com/example/todo/networkinglab/ApplicationTest.java @@ -0,0 +1,13 @@ +package com.example.todo.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..d37c893 --- /dev/null +++ b/NetworkingLab/app/src/main/AndroidManifest.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + diff --git a/NetworkingLab/app/src/main/java/com/example/todo/networkinglab/MainActivity.java b/NetworkingLab/app/src/main/java/com/example/todo/networkinglab/MainActivity.java new file mode 100644 index 0000000..2799ffb --- /dev/null +++ b/NetworkingLab/app/src/main/java/com/example/todo/networkinglab/MainActivity.java @@ -0,0 +1,134 @@ +package com.example.todo.networkinglab; + +import android.content.Context; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; +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 android.widget.TextView; +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.URL; +import java.util.ArrayList; + +public class MainActivity extends AppCompatActivity { + Button mCerealButton, mChocolateButton, mTeaButton; + String mTeaString = "http://api.walmartlabs.com/v1/search?apiKey=wg9r9j94m37sy5e7whn7yy3m&format=json&query=Tea"; + String mCerealString = "http://api.walmartlabs.com/v1/search?apiKey=wg9r9j94m37sy5e7whn7yy3m&format=json&query=Cereal"; + String mChocolateString = "http://api.walmartlabs.com/v1/search?apiKey=wg9r9j94m37sy5e7whn7yy3m&format=json&query=Chocolate"; + ListView mListView; + ArrayAdapter mAdapter; + ArrayList mArrayList; + DownloadTask mTask; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + mCerealButton = (Button) findViewById(R.id.xmlCerealButton); + mChocolateButton = (Button) findViewById(R.id.xmlChocolateButton); + mTeaButton = (Button) findViewById(R.id.xmlTeaButton); + mListView = (ListView) findViewById(R.id.xmlListView); + mArrayList = new ArrayList<>(); + mAdapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,mArrayList); + mListView.setAdapter(mAdapter); + mTask = new DownloadTask(); + + mCerealButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); + NetworkInfo info = manager.getActiveNetworkInfo(); + if (info != null && info.isConnected()) { + if(mTask.getStatus()== AsyncTask.Status.RUNNING){mTask.cancel(true);} + mTask = new DownloadTask(); + mTask.execute(mCerealString); + } else { + Toast.makeText(MainActivity.this, "Check Network connection", Toast.LENGTH_SHORT).show(); + } + } + }); + mChocolateButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); + NetworkInfo info = manager.getActiveNetworkInfo(); + if (info != null && info.isConnected()) { + if(mTask.getStatus()== AsyncTask.Status.RUNNING){mTask.cancel(true);} + mTask = new DownloadTask(); + mTask.execute(mChocolateString); + } else { + Toast.makeText(MainActivity.this, "Check Network connection", Toast.LENGTH_SHORT).show(); + } + } + }); + mTeaButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); + NetworkInfo info = manager.getActiveNetworkInfo(); + if (info != null && info.isConnected()) { + if(mTask.getStatus()== AsyncTask.Status.RUNNING){mTask.cancel(true);} + mTask = new DownloadTask(); + mTask.execute(mTeaString); + } else { + Toast.makeText(MainActivity.this, "Check Network connection", Toast.LENGTH_SHORT).show(); + } + } + }); + } + + public class DownloadTask extends AsyncTask{ + @Override + protected Void doInBackground(String... urls) { + InputStream is; + try { + mArrayList.clear(); + URL url = new URL(urls[0]); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setRequestMethod("GET"); + conn.setDoInput(true); + conn.connect(); + is = conn.getInputStream(); + StringBuilder sb = new StringBuilder(); + BufferedReader br = new BufferedReader(new InputStreamReader(is)); + String contentAsString; + while((contentAsString = br.readLine()) != null) {sb.append(contentAsString);} + contentAsString = sb.toString(); + sb.setLength(0); + JSONObject object = new JSONObject(contentAsString); + JSONArray array = object.optJSONArray("items"); + for (int i = 0; i < array.length(); i++){ + JSONObject repo = array.getJSONObject(i); + String productName = repo.getString("name"); + String productPrice = repo.optString("salePrice"); + sb.append(productName + " $" + productPrice); + mArrayList.add(sb.toString()); + sb.setLength(0);} + } catch (Throwable e){ + e.printStackTrace();} + return null; + } + + @Override + protected void onPostExecute(Void 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..da19c36 --- /dev/null +++ b/NetworkingLab/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,41 @@ + + + + + +