antoniolg / kotlin-for-android-developers Goto Github PK
View Code? Open in Web Editor NEWCompanion App for the book
Home Page: http://antonioleiva.com/kotlin-android-developers/
License: Apache License 2.0
Companion App for the book
Home Page: http://antonioleiva.com/kotlin-android-developers/
License: Apache License 2.0
I have a doubt about the data classes in ServerClasses.tk.
This classes are popullated using GSon. When Gson creates the classes it will use a kind of empty constructor and it will fill the properties, therefore, there might be values that are null (in case the api changes for example). Shouldn't we put default values ensure no null properties?
importing this demo app in the last canary Android Studio, I found some errors during the sync.
please modify the code of "app" like:
compile "org.jetbrains.anko:anko:0.10.1"
compile "org.jetbrains.anko:anko-common:0.10.1"
compile "org.jetbrains.anko:anko-sqlite:0.10.1n"
compile "org.jetbrains.anko:anko-coroutines:0.10.1"
this is enounght to fix the errors of sync
regards
MaX
Where does the id come from?
When I run the app, an error was occurred by create database table DayForecastTable. This is the error:
android.database.sqlite.SQLiteException: near "org": syntax error (code 1): , while compiling: CREATE TABLE IF NOT EXISTS DayForecast
(id INTEGER PRIMARY KEY org.jetbrains.anko.db.SqlTypeModifierImpl@a5038ce, date INTEGER, description TEXT, high INTEGER, low INTEGER, iconUrl TEXT, cityId INTEGER);
Why the AUTOINCREMENT cannot be recognized. My anko version is 0.10.1
Hi i see that you use Anko lib, but i recently read that it is a deprecated library:
https://github.com/Kotlin/anko/blob/master/GOODBYE.md
Have you planned to remove this lib and update your code?
Sentence 3:
Though the one included in the standard librar is a little more complex...
Should be "library"
Paragraph 2, Sentence 1 of 7.3 Extension functions
For instance, we can create a toast function that doesn't the context as parameter, which could ...
Should it be like this:
For instance, we can create a toast function that doesn't need the context as parameter...
(Inserted "need")
Running the code from Chapter 8 branch, the toast never actually occurs. This is the error log I see in Logcat:
11-19 16:57:50.053 5203-5203/? I/zygote: Not late-enabling -Xcheck:jni (already on)
11-19 16:57:50.064 5203-5203/? W/zygote: Unexpected CPU variant for X86 using defaults: x86
11-19 16:57:50.275 5203-5203/com.antonioleiva.weatherapp I/InstantRun: starting instant run server: is main process
11-19 16:57:50.462 5203-5229/com.antonioleiva.weatherapp D/NetworkSecurityConfig: No Network Security Config specified, using platform default
11-19 16:57:50.489 5203-5231/com.antonioleiva.weatherapp D/OpenGLRenderer: HWUI GL Pipeline
[ 11-19 16:57:50.541 1902: 2245 W/ ]
Unrecognized GLES max version string in extensions: ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_dma_v1
11-19 16:57:50.545 5203-5229/com.antonioleiva.weatherapp W/System.err: java.io.FileNotFoundException: http://api.openweathermap.org/data/2.5/forecast/daily?APPID=15646a06818f61f7b8d7823ca833e1ce&q=94043&mode=json&units=metric&cnt=7
11-19 16:57:50.546 5203-5229/com.antonioleiva.weatherapp W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:251)
11-19 16:57:50.551 5203-5229/com.antonioleiva.weatherapp W/System.err: at java.net.URL.openStream(URL.java:1058)
11-19 16:57:50.553 5203-5229/com.antonioleiva.weatherapp W/System.err: at kotlin.io.TextStreamsKt.readBytes(ReadWrite.kt:144)
11-19 16:57:50.562 5203-5229/com.antonioleiva.weatherapp W/System.err: at com.antonioleiva.weatherapp.data.Request.run(Request.kt:9)
11-19 16:57:50.563 5203-5229/com.antonioleiva.weatherapp W/System.err: at com.antonioleiva.weatherapp.ui.activities.MainActivity$onCreate$1.invoke(MainActivity.kt:39)
11-19 16:57:50.563 5203-5229/com.antonioleiva.weatherapp W/System.err: at com.antonioleiva.weatherapp.ui.activities.MainActivity$onCreate$1.invoke(MainActivity.kt:15)
11-19 16:57:50.563 5203-5229/com.antonioleiva.weatherapp W/System.err: at org.jetbrains.anko.AsyncKt$doAsync$1.invoke(Async.kt:140)
11-19 16:57:50.569 5203-5229/com.antonioleiva.weatherapp W/System.err: at org.jetbrains.anko.AsyncKt$doAsync$1.invoke(Unknown Source:0)
11-19 16:57:50.570 5203-5229/com.antonioleiva.weatherapp W/System.err: at org.jetbrains.anko.AsyncKt$sam$Callable$761a5578.call(Unknown Source:2)
11-19 16:57:50.570 5203-5229/com.antonioleiva.weatherapp W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:266)
11-19 16:57:50.571 5203-5229/com.antonioleiva.weatherapp W/System.err: at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
11-19 16:57:50.572 5203-5229/com.antonioleiva.weatherapp W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
11-19 16:57:50.573 5203-5229/com.antonioleiva.weatherapp W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
11-19 16:57:50.574 5203-5229/com.antonioleiva.weatherapp W/System.err: at java.lang.Thread.run(Thread.java:764)
11-19 16:57:51.029 5203-5231/com.antonioleiva.weatherapp I/OpenGLRenderer: Initialized EGL, version 1.4
11-19 16:57:51.029 5203-5231/com.antonioleiva.weatherapp D/OpenGLRenderer: Swap behavior 1
11-19 16:57:51.030 5203-5231/com.antonioleiva.weatherapp W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
11-19 16:57:51.030 5203-5231/com.antonioleiva.weatherapp D/OpenGLRenderer: Swap behavior 0
11-19 16:57:51.032 5203-5231/com.antonioleiva.weatherapp D/EGL_emulation: eglCreateContext: 0xa73850c0: maj 2 min 0 rcv 2
11-19 16:57:51.035 5203-5231/com.antonioleiva.weatherapp D/EGL_emulation: eglMakeCurrent: 0xa73850c0: ver 2 0 (tinfo 0xa73832a0)
[ 11-19 16:57:51.038 1902: 1911 W/ ]
Unrecognized GLES max version string in extensions: ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_dma_v1
11-19 16:57:51.063 5203-5231/com.antonioleiva.weatherapp D/EGL_emulation: eglMakeCurrent: 0xa73850c0: ver 2 0 (tinfo 0xa73832a0)
This becomes a problem when the date of your time zone differs from the date of GMT time.
ForecastByZipCodeRequest
obtains a weekly forecast starting with today in your current time zone.ForecastDb.requestForecastByZipCode(zipCode, todayTimeSpan())
filters out the first row of DayForecastTable
because its column DATE
precedes todayTimeSpan()
.ForecastProvider.requestByZipCode(zipCode, days)
never returns a non-null value.I am in America/New York (EDT GMT -04:00 DST) time zone. Here is a real example when I run the app:
System.currentTimeMillis()
is 1464056054644, or May 23, 2016 at 10:14:14 PM EDT GMT-4:00 DSTtodayTimeSpan()
is 1464048000000, or May 23, 2016 at 8:00:00 PM EDT GMT-4:00 DSTCan't we use Anko for the RecyclerView here?:
Hello, I got a database error and the screen is empty when I first run this app.
Log‘s here : E/SQLiteLog: (1) near "org": syntax error
Crash:
SettingPage
Clear the default zipCode and then exit the current page
In the chapter-19 branch there is a mix-up between zip code and city id. The code compiles and runs because the database is not used. Nonetheless I believe there is an error. Here is what I think needs to change:
In ForecastDataMapper.convertFromDataModel the first parameter in the ForecastList constructor should be "city.id" instead of "zipCode". Additionally, the "zipCode" parameter should be removed from convertFromDataModel so that it takes only a ForecastResult.
Once this change is made the overridden function in RequestForecastCommand must be changed so the "zipCode" parameter is removed from the parameters passed to convertFromDataModel.
This will fix the issue, but I would also recommend changing the name of the "id" parameter on the ForecastList constructor to "cityId" to make it clearer what id this is.
I have been following along in the book. When I added the code for Chapter 19, the DatabaseExtensions.kt file will not compile. To ensure that I didn't type something wrong, I got a zip of the Chapter 19 commit and got the same error:
I'm using buildVersonSdk 25.0.0, targetSdkVersion 25, ext.support_version 25.1.0, ext.kotlin_version 1.0.6, ext_anko_version 0.9 in Android Studio. Everything had worked up to the transition from earlier chapters to chapter 19. Since I'm new to Kotlin, I don't have a clue what is wrong or how to fix it.
First block of code async()
is using the deprecated version instead of doAsync()
.
Starting 9 October 2015, the OpenWeatherMap API requires a valid APPID for access. You just need to register an account with them.
Eg:
http://api.openweathermap.org/data/2.5/forecast/city?id=524901&APPID={APIKEY}
May be an error with android studio but there was no mention of which import was needed here.
I am facing a problem running the app from 'master' , gradle task is failing with these error
I have searched with this error on StackOverFlow . Most of the thread came with solution adding kotlin android plugin , which is already added . Am I missing something @antoniolg
I'm running project on SM-G900F, Android 5.0 and when app loads, the screen is empty, I don't see any data.
Instead of async you have to use doAsync
Hi Antonio,
why are you using commits and not branches to specify different chapters? XD It's only an idea.
import kotlinx.android.synthetic.main.activity_main.*
I could not find where this was automatically to the activity
@antoniolg
I followed your book step by step till chapter 15, and I compiled it on my Nexus 5.
It is an empty screen still.
Here is my build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
androidExtensions {
experimental = true
}
android {
compileSdkVersion 26
buildToolsVersion '26.0.2'
defaultConfig {
applicationId "com.example.user.weatherkotlin"
minSdkVersion 17
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.google.code.gson:gson:2.8.5'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'org.jetbrains.anko:anko:0.10.5'
implementation 'org.jetbrains.anko:anko-common:0.10.5'
implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support:recyclerview-v7:26.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
any thoughts on when it will be production ready?
Okay. So, I started with the tutorial and everything has been going on fine not until I got to chapter 10 trying to implement the Asyn.
doAsync {
val result = RequestForecastCommand("94043").execute()
Log.e(javaClass.simpleName, result.toString()) <-- It doesn't get to this place at all
uiThread() {
Log.e(TAG, "I am here")
Log.e(javaClass.simpleName, result.toString())
forcast_list.adapter = ForcastListAdapter(result)
}
}
Then, I debugged further and checked the ForecastRequest.kt
file
class ForecastRequest(val zipCode: String) {
companion object {
private val APP_ID = "15646a06818f61f7b8d7823ca833e1ce"
private val URL = "http://api.openweathermap.org/data/2.5/" +
"forecast/daily?mode=json&units=metric&cnt=7"
private val COMPLETE_URL = "$URL&APPID=$APP_ID&q="
}
fun execute(): ForecastResult {
val forcastJsonStr = URL(COMPLETE_URL + zipCode).readText()
Log.e(javaClass.simpleName, forcastJsonStr)<-- This printed out a result
return Gson().fromJson(forcastJsonStr, ForecastResult::class.java)
}
}
I don't know why it's not throwing any error or any bug though. And should it be a Gson conversion error, at least I'm supposed to get an error right?
The following code sample does not work anymore:
val forecastList: RecyclerView = find(R.id.forecast_list)
Use this instead:
val forecastList = find<RecyclerView>(R.id.forecast_list)
In MainActivity.kt, starting with Chapter 8, the http link gives me this error in MainActivity->onCreate:
weatherapp W/System.err: java.io.IOException: Cleartext HTTP traffic to api.openweathermap.org not permitted
I propose changing the http://api.openweather... URL to https://api.openweather...
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.