milosev.com
  • Home
    • List all categories
    • Sitemap
  • Downloads
    • WebSphere
    • Hitachi902
    • Hospital
    • Kryptonite
    • OCR
    • APK
  • About me
    • Gallery
      • Italy2022
      • Côte d'Azur 2024
    • Curriculum vitae
      • Resume
      • Lebenslauf
    • Social networks
      • Facebook
      • Twitter
      • LinkedIn
      • Xing
      • GitHub
      • Google Maps
      • Sports tracker
    • Adventures planning
  1. You are here:  
  2. Home
  3. Android

Create Json With Gson

Details
Written by: Stanko Milosev
Category: Android
Published: 06 March 2022
Last Updated: 12 April 2022
Hits: 1859
  • kotlin
Example how to start new Empty activity, add library to Gradle, and create JSON with GSON.

First start "Empty Activity":

In next step in my case I have choosen language Kotlin, Minimum SDK API 21:

In Android -> Gradle Scripts -> build.gradle:

I have added:

    implementation 'com.google.code.gson:gson:2.9.0'
Now my dependecies look like:
dependencies {
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.5.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    implementation 'com.google.code.gson:gson:2.9.0'
}
Go to File -> Sync Project with Gradle Files:

In MainActivity write something like:

internal class Albums {
    var title: String? = null
    var message: String? = null
    var errors = arrayOf<String>()
    var total: String? = null
    var total_pages = 0
    var page = 0
    var limit: String? = null
}
and
val albums = Albums()
albums.title = "Free Music Archive - Albums"
albums.message = ""
albums.total = "11259"
albums.total_pages = 2252
albums.page = 1
albums.limit = "5"

val builder = GsonBuilder()
val gson: Gson = builder.create()
System.out.println(gson.toJson(albums))
My MainActivity now looks like:
package com.example.myapplication

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.google.gson.Gson
import com.google.gson.GsonBuilder

internal class Albums {
    var title: String? = null
    var message: String? = null
    var errors = arrayOf<String>()
    var total: String? = null
    var total_pages = 0
    var page = 0
    var limit: String? = null
}

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val albums = Albums()
        albums.title = "Free Music Archive - Albums"
        albums.message = ""
        albums.total = "11259"
        albums.total_pages = 2252
        albums.page = 1
        albums.limit = "5"

        val builder = GsonBuilder()
        val gson: Gson = builder.create()
        System.out.println(gson.toJson(albums))
    }
}

New and improved my list of best applications

Details
Written by: Stanko Milosev
Category: Android
Published: 04 January 2021
Last Updated: 09 November 2021
Hits: 2106
Here I already gave one list of android application which I was using. Here is new one:

Waze
WhatsApp
Sports Tracker
Facebook
Twitter
OpenVPN Connect
Remote Desktop 8
Komoot
Shazam
TubeMate
Equalizer + Pro
TeamViewer
VLC
Contacts
Google Calendar
Microsoft Teams
Google Home
Solid Explorer File Manager

New line

Details
Written by: Stanko Milosev
Category: Android
Published: 15 April 2016
Last Updated: 15 April 2016
Hits: 4250

Recently I had a problem that I needed to send some text from Android over webview to JavaScript, but with new lines included, problem was that webview didn't respect \r\n.

This is my solution:

...
receivedPacket = new DatagramPacket(buf, buf.length);
...
final String strReceivedPacket = new String(receivedPacket.getData(), 0, receivedPacket.getLength()).replaceAll("\r\n", "\\\\r\\\\n");

Command line building and installing - gradle and adb

Details
Written by: Stanko Milosev
Category: Android
Published: 08 March 2016
Last Updated: 08 March 2016
Hits: 4991

Just a few comments on my investigation about building and installing Android application.

In Linux go to root folder of your application and write something like:

./gradlew build

build means build.gradle actually, or something like ./gradlew assembleDebug, with that you will build application with debug type. Check the source in /app/build.gradle.

After building we can install application with command like:

adb install -d -r myApp.apk

Where:

-d Direct an adb command to the only attached USB device.
-r Reinstall an exisiting app, keeping its data.

  1. Border around controls
  2. AsyncTask
  3. Example of buttons one under another one
  4. Align button to the bottom of page

Page 7 of 11

  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11