Skip to content

Commit e1a2e02

Browse files
committed
Add Android volley sample
1 parent 62f304b commit e1a2e02

File tree

33 files changed

+614
-0
lines changed

33 files changed

+614
-0
lines changed

gradle/android-volley/.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/workspace.xml
5+
/.idea/libraries
6+
.DS_Store
7+
/build
8+
/captures
9+
.externalNativeBuild
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
apply plugin: 'com.android.application'
2+
apply plugin: 'kotlin-android'
3+
4+
android {
5+
compileSdkVersion 25
6+
buildToolsVersion "25.0.3"
7+
defaultConfig {
8+
applicationId "org.example.kotlin.volley"
9+
minSdkVersion 24
10+
targetSdkVersion 25
11+
versionCode 1
12+
versionName "1.0"
13+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
14+
}
15+
buildTypes {
16+
release {
17+
minifyEnabled false
18+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
19+
}
20+
}
21+
}
22+
23+
dependencies {
24+
compile fileTree(dir: 'libs', include: ['*.jar'])
25+
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
26+
exclude group: 'com.android.support', module: 'support-annotations'
27+
})
28+
compile 'com.android.support:appcompat-v7:25.3.1'
29+
testCompile 'junit:junit:4.12'
30+
compile 'com.android.support.constraint:constraint-layout:1.0.1'
31+
compile 'com.android.support:recyclerview-v7:25.3.1'
32+
compile 'com.android.volley:volley:1.0.0'
33+
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
34+
}
35+
36+
repositories {
37+
mavenCentral()
38+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /Users/4u7/Library/Android/sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
18+
19+
# Uncomment this to preserve the line number information for
20+
# debugging stack traces.
21+
#-keepattributes SourceFile,LineNumberTable
22+
23+
# If you keep the line number information, uncomment this to
24+
# hide the original source file name.
25+
#-renamesourcefileattribute SourceFile
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.example.kotlin.volley
2+
3+
import android.content.Context
4+
import android.support.test.InstrumentationRegistry
5+
import android.support.test.runner.AndroidJUnit4
6+
7+
import org.junit.Test
8+
import org.junit.runner.RunWith
9+
10+
import org.junit.Assert.*
11+
12+
/**
13+
* Instrumentation test, which will execute on an Android device.
14+
15+
* @see [Testing documentation](http://d.android.com/tools/testing)
16+
*/
17+
@RunWith(AndroidJUnit4::class)
18+
class ExampleInstrumentedTest {
19+
@Test
20+
@Throws(Exception::class)
21+
fun useAppContext() {
22+
// Context of the app under test.
23+
val appContext = InstrumentationRegistry.getTargetContext()
24+
25+
assertEquals("org.example.kotlin.volley", appContext.packageName)
26+
}
27+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="org.example.kotlin.volley">
4+
5+
<uses-permission android:name="android.permission.INTERNET"/>
6+
7+
<application
8+
android:name=".App"
9+
android:allowBackup="true" android:icon="@mipmap/ic_launcher"
10+
android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round"
11+
android:supportsRtl="true" android:theme="@style/AppTheme">
12+
<activity android:name=".MainActivity">
13+
<intent-filter>
14+
<action android:name="android.intent.action.MAIN" />
15+
16+
<category android:name="android.intent.category.LAUNCHER" />
17+
</intent-filter>
18+
</activity>
19+
</application>
20+
21+
</manifest>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.example.kotlin.volley
2+
3+
import android.app.Application
4+
5+
6+
class App : Application() {
7+
override fun onCreate() {
8+
super.onCreate()
9+
VolleyService.initialize(this)
10+
}
11+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.example.kotlin.volley
2+
3+
import android.graphics.Bitmap
4+
import android.util.LruCache
5+
import com.android.volley.toolbox.ImageLoader
6+
7+
class LruBitmapCache constructor(sizeInKB: Int = defaultLruCacheSize):
8+
LruCache<String, Bitmap>(sizeInKB), ImageLoader.ImageCache {
9+
10+
override fun sizeOf(key: String, value: Bitmap): Int = value.rowBytes * value.height / 1024
11+
12+
override fun getBitmap(url: String): Bitmap? {
13+
return get(url)
14+
}
15+
16+
override fun putBitmap(url: String, bitmap: Bitmap) {
17+
put(url, bitmap)
18+
}
19+
20+
companion object {
21+
val defaultLruCacheSize: Int
22+
get() {
23+
val maxMemory = (Runtime.getRuntime().maxMemory() / 1024).toInt()
24+
val cacheSize = maxMemory / 8
25+
return cacheSize
26+
}
27+
}
28+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package org.example.kotlin.volley
2+
3+
import android.os.Bundle
4+
import android.support.v7.app.AppCompatActivity
5+
import android.support.v7.widget.LinearLayoutManager
6+
import android.support.v7.widget.RecyclerView
7+
import android.text.Html
8+
import android.view.LayoutInflater
9+
import android.view.View
10+
import android.view.ViewGroup
11+
import android.widget.TextView
12+
import android.widget.Toast
13+
import com.android.volley.Request
14+
import com.android.volley.Response
15+
import com.android.volley.toolbox.JsonObjectRequest
16+
import com.android.volley.toolbox.NetworkImageView
17+
import org.json.JSONArray
18+
import org.json.JSONObject
19+
20+
21+
class MainActivity : AppCompatActivity() {
22+
23+
override fun onCreate(savedInstanceState: Bundle?) {
24+
super.onCreate(savedInstanceState)
25+
setContentView(R.layout.activity_main)
26+
27+
val newsRecyclerView = findViewById(R.id.news_list) as RecyclerView
28+
newsRecyclerView.layoutManager = LinearLayoutManager(this)
29+
30+
val url = "http://query.yahooapis.com/v1/public/yql?q=" +
31+
"select * from rss where url='http://rss.news.yahoo.com/rss/topstories'" +
32+
"&format=json"
33+
34+
val request = JsonObjectRequest(Request.Method.GET, url, null,
35+
Response.Listener<JSONObject> { response ->
36+
val news = response
37+
.getJSONObject("query")
38+
.getJSONObject("results")
39+
.getJSONArray("item")
40+
41+
newsRecyclerView.adapter = NewsAdapter(news)
42+
},
43+
Response.ErrorListener {
44+
Toast.makeText(this, "That didn't work!", Toast.LENGTH_SHORT).show()
45+
})
46+
47+
VolleyService.requestQueue.add(request)
48+
VolleyService.requestQueue.start()
49+
}
50+
51+
class NewsAdapter(val news: JSONArray) : RecyclerView.Adapter<NewsViewHolder>() {
52+
override fun onBindViewHolder(holder: NewsViewHolder, position: Int) {
53+
holder.bind(news.getJSONObject(position), position)
54+
}
55+
56+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NewsViewHolder {
57+
val layoutInflater = LayoutInflater.from(parent.context)
58+
val view = layoutInflater.inflate(R.layout.news_item, parent, false)
59+
return NewsViewHolder(view)
60+
}
61+
62+
override fun getItemCount(): Int = news.length()
63+
}
64+
65+
class NewsViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
66+
fun bind(newsItem: JSONObject, position: Int) {
67+
val title = view.findViewById(R.id.newsTitle) as TextView
68+
val image = view.findViewById(R.id.image) as NetworkImageView
69+
title.text = Html.fromHtml(newsItem["title"].toString(), 0)
70+
image.setImageUrl(
71+
"http://loremflickr.com/800/600/cat?random=$position",
72+
VolleyService.imageLoader)
73+
}
74+
}
75+
}
76+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.example.kotlin.volley
2+
3+
import android.content.Context
4+
import com.android.volley.RequestQueue
5+
import com.android.volley.toolbox.ImageLoader
6+
import com.android.volley.toolbox.Volley
7+
8+
9+
object VolleyService {
10+
private lateinit var context: Context
11+
12+
val requestQueue: RequestQueue by lazy { Volley.newRequestQueue(context) }
13+
14+
val imageLoader: ImageLoader by lazy { ImageLoader(requestQueue, LruBitmapCache()) }
15+
16+
fun initialize(context: Context) {
17+
this.context = context.applicationContext
18+
}
19+
}

0 commit comments

Comments
 (0)