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 .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 .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 .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 .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 .idea/gradle.xml

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

46 changes: 46 additions & 0 deletions .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 .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 .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 .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 app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
26 changes: 26 additions & 0 deletions 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 "com.farazfazli.walmartassist"
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 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/faraz/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 com.farazfazli.walmartassist;

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);
}
}
24 changes: 24 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.farazfazli.walmartassist"
xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<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>
164 changes: 164 additions & 0 deletions app/src/main/java/com/farazfazli/walmartassist/MainActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package com.farazfazli.walmartassist;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
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.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

private static final String TAG = "[" + MainActivity.class.getCanonicalName() + "]";

private static final String API_KEY = "INSERTAPIKEYHERE";
private static final String BASE_URL = "http://api.walmartlabs.com/v1/search?query=";
private static final String END_URL = "&format=json&apiKey=" + API_KEY;

private GrabWalmartJSONDataAsyncTask grabWalmartJSONDataAsyncTask;

private ArrayList<String> mItemsArrayList;
private ArrayAdapter<String> mArrayAdapter;

// API Call: BASE_URL + mQuery + END_URL

private String mQuery = "";
private String mResponse = "";

private ListView mItemsListView;
private ProgressBar mProgressBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mItemsArrayList = new ArrayList<String>();
mArrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mItemsArrayList);

mProgressBar = (ProgressBar) findViewById(R.id.progressBar);

mItemsListView = (ListView) findViewById(R.id.itemsListView);
mItemsListView.setAdapter(mArrayAdapter);

Button mCerealButton = (Button) findViewById(R.id.cereal);
Button mChocolateButton = (Button) findViewById(R.id.chocolate);
Button mTeaButton = (Button) findViewById(R.id.tea);


View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i(TAG, "Tapped!");
mArrayAdapter.clear();
mQuery = ((Button) view).getText().toString();
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) { // has internet
if(grabWalmartJSONDataAsyncTask == null) { // first run
grabWalmartJSONDataAsyncTask = new GrabWalmartJSONDataAsyncTask();
}
if(grabWalmartJSONDataAsyncTask.getStatus() != AsyncTask.Status.RUNNING) {
grabWalmartJSONDataAsyncTask = new GrabWalmartJSONDataAsyncTask(); // must create another one
grabWalmartJSONDataAsyncTask.execute();
} else {
Log.i(TAG, "Already running!");
Toast.makeText(MainActivity.this, "Already running!", Toast.LENGTH_SHORT).show();
}
} else {
Log.i(TAG, "No internet!");
Toast.makeText(MainActivity.this, "No internet!", Toast.LENGTH_SHORT).show();
}
}
};

mCerealButton.setOnClickListener(listener);
mChocolateButton.setOnClickListener(listener);
mTeaButton.setOnClickListener(listener);
}

private String getInputData(InputStream inputStream) throws IOException {
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

String body;
while ((body = reader.readLine()) != null) {
builder.append(body);
}

reader.close();

return builder.toString();
}

public class GrabWalmartJSONDataAsyncTask extends AsyncTask<Void, Void, Void> {

@Override
protected void onPreExecute() {
mItemsListView.setVisibility(View.INVISIBLE);
mProgressBar.setVisibility(View.VISIBLE);
super.onPreExecute();
}

@Override
protected Void doInBackground(Void... params) {
Log.i(TAG, "doInBackground started!");
try {
URL url = new URL(BASE_URL + mQuery + END_URL);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
mResponse = getInputData(inputStream);
convertResponseToJsonData(mResponse);
} catch (MalformedURLException e) {
e.printStackTrace();
Log.e(TAG, "MalformedURLException!");
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "IOException!");
} catch (JSONException e) {
e.printStackTrace();
Log.e(TAG, "JSON Exception!");
}
return null;
}

@Override
protected void onPostExecute(Void aVoid) {
mItemsListView.setVisibility(View.VISIBLE);
mProgressBar.setVisibility(View.INVISIBLE);
Log.i(TAG, mItemsArrayList.toString());
mArrayAdapter.notifyDataSetChanged();
super.onPostExecute(aVoid);
}
}

private void convertResponseToJsonData(String response) throws JSONException {
JSONObject object = new JSONObject(response); // entire object
JSONArray array = object.getJSONArray("items"); // items array
for (int i = 0; i < array.length(); i++) {
JSONObject currentObject = array.getJSONObject(i);
String currentItem = currentObject.getString("name"); // specific item name
mItemsArrayList.add(currentItem);
}
}
}
Loading