From 9e12544bf8827bdba070f6eec629eca1d6664740 Mon Sep 17 00:00:00 2001 From: adityosagir Date: Thu, 25 Feb 2016 12:11:46 -0500 Subject: [PATCH] Adityo - Networking-in-Android --- MyApplication/.gitignore | 8 + MyApplication/.idea/.name | 1 + MyApplication/.idea/compiler.xml | 22 +++ .../.idea/copyright/profiles_settings.xml | 3 + 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 ++ .../asagir/myapplication/ApplicationTest.java | 13 ++ .../app/src/main/AndroidManifest.xml | 23 +++ .../asagir/myapplication/MainActivity.java | 141 +++++++++++++++ .../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 ++ .../asagir/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 + 34 files changed, 745 insertions(+) create mode 100755 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/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 100755 MyApplication/app/.gitignore create mode 100755 MyApplication/app/build.gradle create mode 100755 MyApplication/app/proguard-rules.pro create mode 100755 MyApplication/app/src/androidTest/java/com/example/asagir/myapplication/ApplicationTest.java create mode 100755 MyApplication/app/src/main/AndroidManifest.xml create mode 100755 MyApplication/app/src/main/java/com/example/asagir/myapplication/MainActivity.java create mode 100755 MyApplication/app/src/main/res/layout/activity_main.xml create mode 100755 MyApplication/app/src/main/res/mipmap-hdpi/ic_launcher.png create mode 100755 MyApplication/app/src/main/res/mipmap-mdpi/ic_launcher.png create mode 100755 MyApplication/app/src/main/res/mipmap-xhdpi/ic_launcher.png create mode 100755 MyApplication/app/src/main/res/mipmap-xxhdpi/ic_launcher.png create mode 100755 MyApplication/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png create mode 100755 MyApplication/app/src/main/res/values-w820dp/dimens.xml create mode 100755 MyApplication/app/src/main/res/values/colors.xml create mode 100755 MyApplication/app/src/main/res/values/dimens.xml create mode 100755 MyApplication/app/src/main/res/values/strings.xml create mode 100755 MyApplication/app/src/main/res/values/styles.xml create mode 100755 MyApplication/app/src/test/java/com/example/asagir/myapplication/ExampleUnitTest.java create mode 100755 MyApplication/build.gradle create mode 100755 MyApplication/gradle.properties create mode 100755 MyApplication/gradle/wrapper/gradle-wrapper.jar create mode 100755 MyApplication/gradle/wrapper/gradle-wrapper.properties create mode 100755 MyApplication/gradlew create mode 100755 MyApplication/gradlew.bat create mode 100755 MyApplication/settings.gradle diff --git a/MyApplication/.gitignore b/MyApplication/.gitignore new file mode 100755 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..1046d98 --- /dev/null +++ b/MyApplication/.idea/.name @@ -0,0 +1 @@ +MyApplication \ 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/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..cca2cda --- /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 100755 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 100755 index 0000000..900b1a9 --- /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.asagir.myapplication" + 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 100755 index 0000000..07767ca --- /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/adi/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/asagir/myapplication/ApplicationTest.java b/MyApplication/app/src/androidTest/java/com/example/asagir/myapplication/ApplicationTest.java new file mode 100755 index 0000000..4e1fb00 --- /dev/null +++ b/MyApplication/app/src/androidTest/java/com/example/asagir/myapplication/ApplicationTest.java @@ -0,0 +1,13 @@ +package com.example.asagir.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 100755 index 0000000..63edf44 --- /dev/null +++ b/MyApplication/app/src/main/AndroidManifest.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + diff --git a/MyApplication/app/src/main/java/com/example/asagir/myapplication/MainActivity.java b/MyApplication/app/src/main/java/com/example/asagir/myapplication/MainActivity.java new file mode 100755 index 0000000..084cad7 --- /dev/null +++ b/MyApplication/app/src/main/java/com/example/asagir/myapplication/MainActivity.java @@ -0,0 +1,141 @@ +package com.example.asagir.myapplication; + +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.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 mCerealButton; + Button mChocolateButton; + Button mTeaButton; + + private ListView mListView; + private ArrayList mStringArray; + private ArrayAdapter mAdapter; + + String cerealUrl = "http://api.walmartlabs.com/v1/search?query=cereal&format=json&apiKey=9xhfjf2aj78axy7u43f3rv2e"; + String chocolateUrl = "http://api.walmartlabs.com/v1/search?query=chocolate&format=json&apiKey=9xhfjf2aj78axy7u43f3rv2e"; + String teaUrl = "http://api.walmartlabs.com/v1/search?query=tea&format=json&apiKey=9xhfjf2aj78axy7u43f3rv2e"; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + mCerealButton = (Button) findViewById(R.id.ceralButton); + mChocolateButton = (Button) findViewById(R.id.chocolateButton); + mTeaButton = (Button) findViewById(R.id.teaButton); + + mStringArray = new ArrayList(); + + mListView = (ListView) findViewById(R.id.listView); + mAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, mStringArray); + + mListView.setAdapter(mAdapter); + + mCerealButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + DownloadAsyncTask downloadAsyncTask = new DownloadAsyncTask(); + downloadAsyncTask.execute(cerealUrl); + } + }); + + mChocolateButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + DownloadAsyncTask downloadAsyncTask = new DownloadAsyncTask(); + downloadAsyncTask.execute(chocolateUrl); + } + }); + + mTeaButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + DownloadAsyncTask downloadAsyncTask = new DownloadAsyncTask(); + downloadAsyncTask.execute(teaUrl); + } + }); + } + + private String getInputData(InputStream inStream) throws IOException { + StringBuilder stringBuilder = new StringBuilder(); + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inStream)); + + String cerealData; + + while ((cerealData = bufferedReader.readLine()) != null) { + stringBuilder.append(cerealData); + } + + bufferedReader.close(); + + return stringBuilder.toString(); + + } + + public class DownloadAsyncTask extends AsyncTask { + @Override + protected String doInBackground(String... urls) { + String data = ""; + try { + URL url = new URL(urls[0]); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestProperty("Content-Type", "application/json"); +// connection.setRequestMethod("GET"); +// connection.setDoInput(true); + connection.connect(); + + InputStream inStream = connection.getInputStream(); + + data = getInputData(inStream); + } catch (Throwable throwable) { + throwable.printStackTrace(); + } + + try { + JSONObject dataObject = new JSONObject(data); + + JSONArray cerealObject = dataObject.getJSONArray("items"); + + mStringArray.clear(); + + for (int i = 0; i < cerealObject.length(); i++) { + JSONObject object = cerealObject.optJSONObject(i); + String name = object.getString("name"); + + mStringArray.add(name); + } + } catch (JSONException exception) { + exception.printStackTrace(); + } + + return data; + } + + @Override + protected void onPostExecute(String data) { + super.onPostExecute(data); + + 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 100755 index 0000000..de53d80 --- /dev/null +++ b/MyApplication/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,39 @@ + + + +