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 NetworkingLab/.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 NetworkingLab/.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 NetworkingLab/.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 NetworkingLab/.idea/copyright/profiles_settings.xml

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

6 changes: 6 additions & 0 deletions NetworkingLab/.idea/encodings.xml

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

19 changes: 19 additions & 0 deletions NetworkingLab/.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 NetworkingLab/.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 NetworkingLab/.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 NetworkingLab/.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 NetworkingLab/.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 NetworkingLab/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
26 changes: 26 additions & 0 deletions NetworkingLab/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.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'
}
17 changes: 17 additions & 0 deletions NetworkingLab/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 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 *;
#}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.todo.networkinglab;

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

<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>
Original file line number Diff line number Diff line change
@@ -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<String> mAdapter;
ArrayList<String> 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<String>(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<String,Void,Void>{
@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();
}
}
}
Loading