Unit 4
Unit 4
… 04.1 Concept and Features of JSON, Similarities and difference among JSON and
XML
4.2 JSON objects (with string and Numbers))
4.3 JSON Arrays and their examples:
4.3.1 Array of string, Array of Numbers, Array of Booleans, Array of objects, Multi-
Dimensional Arrays
4.3.2 JSON comments
4.4 Building multi-screen apps:
4.4.1 Intents and their applications, types of intents,
4.4.2 Data exchange from one activity to another using intent
4.5 Working with implicit intents:
4.5.1 Opening web URLs through app
4.5.2 Sharing media from our app to other apps
What is json?
● JSON stands for JavaScript object notation. JSON has been derived from
javascript, where javascript is a programming language. It was originally created
to hold the structured data that could be used in javascript. JSON became so
popular that it is used for data for all kinds of applications. It is the most popular
way of sending the data for Web APIs.
● The JSON format was originally specified by Douglas Crockford, and is
described in RFC 4627(The application/json Media Type for JavaScript
Object Notation (JSON)).
● It use within various programming languages such as PHP, PERL, Python, Ruby,
Java, etc.
● It is commonly use for Api and Configuration.
○ Booleans: The Boolean value could be either true or false without any quotation
marks.
JSON Example
{"employees":[
{ "firstName":"John", "lastName":"Doe" },
{ "firstName":"Anna", "lastName":"Smith" },
{ "firstName":"Peter", "lastName":"Jones" }
]}
JSON XML
JSON stands for javascript object XML stands for an extensible markup
notation. language.
The extension of the json file is .json. The extension of the xml file is .xml.
It does not have any capacity to XML is a markup language, so it has the
display the data. capacity to display the content.
JSON is quicker to read and write. XML file takes time to read and write
because the learning curve is higher.
JSON can use arrays to represent the XML does not contain the concept of arrays.
data.
{"employees":[ <employees>
{ "firstName":"John", "lastName":"Doe" <employee>
}, <firstName>John</firstName>
{ "firstName":"Anna", <lastName>Doe</lastName>
"lastName":"Smith" }, </employee>
{ "firstName":"Peter", <employee>
"lastName":"Jones" } <firstName>Anna</firstName>
]} <lastName>Smith</lastName>
</employee>
<employee>
<firstName>Peter</firstName>
<lastName>Jones</lastName>
</employee></employees>
❖ JSON objects
JSON object literals are surrounded by curly braces {}.
Keys must be strings, and values must be a valid JSON data type:
● string
● number
● object
● array
● boolean
● null
{
JSON Object with Numbers "integer": 34,
SON supports numbers in double
"fraction": .2145,
precision floating-point format. The
number can be digits (0-9), fractions (.33, "exponent": 6.61789e+0 }
.532 etc) and exponents (e, e+, e-,E, E+,
E-).
{
JSON Object with Booleans "first": true,
employee:{
JSON Nested Object Example
"firstName": "Sonoo",
"lastName": "Jaiswal",
"age": 27,
UNIT- 4 JSON CONCEPT
"address" : {
"city": "Ghaziabad",
"state": "UP",
"postalCode": 201007
★JSON Array
JSON array represents ordered list of values. JSON array can store multiple
values. It can store string, number, boolean or object in JSON array.
{"employees":[
➢Multidimensional Array
Creating a multi-dimensional array
"employee": {
"name": "Bob",
UNIT- 4 JSON CONCEPT
"salary": 56000,
○ Launch an activity
○ Broadcast a message
UNIT- 4 JSON CONCEPT
Methods Description
➢ Explicit Intent−
○ It is going to connect the internal world of an application such as start activity or
send data between two activities. To start new activity we have to create Intent
object and pass source activity and destination activity as shown below:
➢Android calling one activity from another activity example
What is the difference between a bundle and an intent?
Bundles are used with intent and values are sent and retrieved in the same fashion, as
it is done in the case of Intent. It depends on the user what type of values the user
wants to pass, but bundles can hold all types of values (int, String, boolean, char) and
activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="10dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/nameEt"
android:hint="Enter Name"
android:inputType="text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
UNIT- 4 JSON CONCEPT
<EditText
android:id="@+id/emailEt"
android:hint="Enter Email"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/phoneEt"
android:hint="Enter Phone"
android:inputType="phone"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/saveBtn"
android:text="save"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
UNIT- 4 JSON CONCEPT
</LinearLayout>
MainActivity.kt
Add the following code in the MainActivity.kt class. In this class,
we are creating an instance of Intent class and calling the
component activity class SecondActivity.kt. The putExtra(key,
value) method of Intent class send the data to the
SecondActivity.kt class. The startActivity() method starts the
Intent.
package com.example.explicit_intent
import androidx.appcompat.app.AppCompatActivity
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
saveBtn.setOnClickListener {
intent.putExtra("Name", name)
intent.putExtra("Email", email)
intent.putExtra("Phone", phone)
startActivity(intent)
*Note - SecondActivity is the JAVA class name where the activity will
now be navigated.
It is Kotlin Reflection API, that can handle Kotlin features like properties, data
classes, etc. By using ::class. java , you get an instance of Class. It is Java
Reflection API, that interops with any Java reflection code, but can't work with
some Kotlin features.
UNIT- 4 JSON CONCEPT
second_activity.xml
In the second_activity.xml file add the following code.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="10dp"
tools:context=".MainActivity2">
<TextView
android:id="@+id/resultTv"
android:textSize="30sp"
UNIT- 4 JSON CONCEPT
android:textStyle="bold"
android:textColor="#000"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
SecondActivity.kt
Add the following code in the SecondActivity.kt class. In this
class, we are receiving the intent data using creating the instance
on Bundle class using intent.extras and displaying the data in
toast message. By clicking on the button, we are invoking Intent
to call MainActivity.kt class.
package com.example.explicit_intent
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
UNIT- 4 JSON CONCEPT
import android.widget.Button
import android.widget.TextView
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
//textview
//setText
UNIT- 4 JSON CONCEPT
❖ Implicit Intents −
It going to connect with out side application such as call, mail, phone,see any
website ..etc. In implicit intent we have to pass an action using setAction() as
shown below example.
❖ Following is the simple code snippet of implicit intent in the android application.
intent = Intent(Intent.ACTION_VIEW)
intent.setData(Uri.parse("https://www.amrolicollege.org/"))
startActivity(intent)
UNIT- 4 JSON CONCEPT
startActivity(intent)
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="example.javatpoint.com.kotlinimplicitintent.MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
UNIT- 4 JSON CONCEPT
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.172" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
UNIT- 4 JSON CONCEPT
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.77" />
</android.support.constraint.ConstraintLayout>
MainActivity.kt
Add the following code in the MainActivity.kt class. In this class, we are invoking the
URL on clicking the button using Implicit Intent. To invoke this intent, we are passing the
action type and URL. The startActivity() method is used to start the Intent.
package example.javatpoint.com.kotlinimplicitintent
import android.content.Intent
import android.net.Uri
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
UNIT- 4 JSON CONCEPT
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener(){
intent = Intent(Intent.ACTION_VIEW)
intent.setData(Uri.parse("https://www.amrolicollege.org/"))
startActivity(intent)
/* intent= Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.amrolicollege.org/"))
startActivity(intent)*/
Output:
UNIT- 4 JSON CONCEPT
★ Uri is the sequence of the characters used to identify resources uniquely over the
internet.
★ What is Setdata?
- setData() method sets the drag operation's drag data to the specified data and
type.
Intent requests
In order to launch Google Maps with an intent you must first create an Intent object,
specifying its action, URI and package.
● Action: All Google Maps intents are called as a View action — ACTION_VIEW.
● URI: Google Maps intents use URL encoded that specify a desired action, along
with some data with which to perform the action.
● Package: Calling setPackage("com.google.android.apps.maps") will ensure that
the Google Maps app for Android handles the Intent. If the package isn't set, the
system will determine which apps can handle the Intent. If multiple apps are
available, the user may be asked which app they would like to use.
button.setOnClickListener(View.OnClickListener {
})
UNIT- 4 JSON CONCEPT
Examples:2
button.setOnClickListener(View.OnClickListener {
val gmmIntentUri =
Uri.parse("google.navigation:q=+BAPS+Akshardham+Temple
,+Gandhinagar+India")
mapIntent.setPackage("com.google.android.apps.maps")
startActivity(mapIntent)
})
Example:3
ACTIVITY1.XML
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginTop="18dp"
android:layout_marginRight="22dp" />
<EditText
android:id="@+id/editText2"
UNIT- 4 JSON CONCEPT
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_alignLeft="@+id/editText1"
android:layout_marginTop="20dp" />
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_alignLeft="@+id/editText2"
android:layout_marginTop="30dp" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText1"
android:layout_alignBottom="@+id/editText1"
android:layout_alignParentLeft="true"
android:text="Send To:"
android:textColor="#0F9D58" />
UNIT- 4 JSON CONCEPT
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText2"
android:layout_alignBottom="@+id/editText2"
android:layout_alignParentLeft="true"
android:text="Email Subject:"
android:textColor="#0F9D58" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText3"
android:layout_alignBottom="@+id/editText3"
android:text="Email Body:"
android:textColor="#0F9D58" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
UNIT- 4 JSON CONCEPT
android:layout_below="@+id/editText3"
android:layout_alignLeft="@+id/editText3"
android:layout_marginLeft="76dp"
android:layout_marginTop="20dp"
</RelativeLayout>
package com.example.send_email
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.example.send_email.ui.theme.Send_emailTheme
super.onCreate(savedInstanceState)
setContentView(R.layout.activity1)
button.setOnClickListener {
intent.putExtra(Intent.EXTRA_EMAIL, arrayOf(emailsend))
intent.putExtra(Intent.EXTRA_SUBJECT, emailsubject)
intent.putExtra(Intent.EXTRA_TEXT, emailbody)
intent.type = "message/rfc822"
MainActivity.kt
package com.example.implicit_intent
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.provider.AlarmClock
import android.provider.MediaStore
import android.view.View
import android.widget.Button
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
UNIT- 4 JSON CONCEPT
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import
com.example.implicit_intent.ui.theme.Implicit_intentTheme
//camera
val intent =
Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivity(intent)
})
})
}