Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions network/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
1 change: 1 addition & 0 deletions network/.idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions network/.idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions network/.idea/copyright/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions network/.idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 62 additions & 0 deletions network/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions network/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions network/.idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions network/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions network/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
26 changes: 26 additions & 0 deletions network/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
applicationId "ypsitos.walmartapiapp"
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'
}
17 changes: 17 additions & 0 deletions network/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/YPsitos/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 *;
#}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ypsitos.walmartapiapp;

import android.app.Application;
import android.test.ApplicationTestCase;

/**
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
*/
public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class);
}
}
22 changes: 22 additions & 0 deletions network/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ypsitos.walmartapiapp">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
146 changes: 146 additions & 0 deletions network/app/src/main/java/ypsitos/walmartapiapp/MainActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package ypsitos.walmartapiapp;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
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;

public class MainActivity extends AppCompatActivity {
Button mTeaButton;
Button mChocolateButton;
Button mCerealButton;
URL mTeaUrl;
URL mChocolateUrl;
URL mCerealUrl;
ListView mListView;
ArrayList<String> mStringArray;
ArrayAdapter<String> mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
mTeaUrl = new URL("http://api.walmartlabs.com/v1/search?query=tea&format=json&apiKey=8u2tky93nyckurd5txzkqec4");
mChocolateUrl = new URL("http://api.walmartlabs.com/v1/search?query=chocolate&format=json&apiKey=8u2tky93nyckurd5txzkqec4");
mCerealUrl = new URL("http://api.walmartlabs.com/v1/search?query=cereal&format=json&apiKey=8u2tky93nyckurd5txzkqec4");
} catch (MalformedURLException e) {
e.printStackTrace();
}

mTeaButton = (Button)findViewById(R.id.teaButton);
mTeaButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
WalmartTask walmartTask = new WalmartTask();
walmartTask.execute(mTeaUrl.toString());
}
});


mChocolateButton = (Button)findViewById(R.id.chocolateButton);
mChocolateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
WalmartTask walmartTask = new WalmartTask();
walmartTask.execute(mChocolateUrl.toString());
}
});

mCerealButton = (Button)findViewById(R.id.cerealButton);
mCerealButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
WalmartTask walmartTask = new WalmartTask();
walmartTask.execute(mCerealUrl.toString());
}
});

ArrayList<String> mStringArray;
ArrayAdapter<String> mAdapter;

mStringArray = new ArrayList<String>();
mListView = (ListView) findViewById(R.id.listView1);
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStringArray);
mListView.setAdapter(mAdapter);
}
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 WalmartTask extends AsyncTask<String, Void, String>{
@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 thr) {
thr.printStackTrace();
}

try {
JSONObject dataObject = new JSONObject(data);
JSONArray itemJsonArray = dataObject.getJSONArray("name");
mStringArray.clear();
for (int i = 0; i < itemJsonArray.length(); i++){
JSONObject object = itemJsonArray.optJSONObject(i);
String name = object.optString("name");
mStringArray.add(name);
}
} catch (JSONException e) {
e.printStackTrace();
}
return data;
}​
@Override
protected void onPostExecute(String data) {
super.onPostExecute(data);
mAdapter.notifyDataSetChanged();
}
}
​}
Loading