Wednesday, 31 May 2017

Android JSONObject String Parsing Explanations

While parsing JSONObject String we will use below methods

1. has("key_name");
it used for check specific KEY is exists or not
if key exists in jsonObject it returns TRUE otherwise it returns FALSE.
                            
2. isNull("key_name");
it used for check specific OBJECT exists or not, if exists it
checks specific object is NULL or not
if key not exists or exists but equals NULL it returns TRUE otherwise it returns FALSE.
                            
3. keys();
Used for get all keys exists from JSON
                            
4. getBoolean("key_name");
Used for get boolean value from JSON;
                            
5. getInt("key_name");
Used for get Integer value from JSON;
                            
6. getLong("key_name");
Used for get Long value from JSON;
                            
7. getDouble("key_name");
Used for get Double value from JSON;
                            
8. getString("key_name");
Used for get String value from JSON;
                            
9.get("key_name");
Used for get Object value from JSON;
                            
10. getJSONObject("key_name");
Used for get JSONObject value from JSON;
                            
11. getJSONObject("key_name");
Used for get JSONArray value from JSON;

------------------------------------------------------------
Sample JSON String:

{"boolean_key":true,"int_key":42798,"long_key":5349,"double_key":70.25,"string_key":"ganesh","json_object_key":{"name":"venkat","phno":"0123456789","city":"Hyd"},"json_array_key":[50,60,70,80]}

------------------------------------------------------------

1. has("key_name");

Syntax:
jsonObject.has("key_name");
it used for check specific KEY is exists or not
if sno key exists in jsonObject it returns TRUE otherwise it returns FALSE.

Example
boolean b1 = jsonObject.has("string_key");
boolean b2 = jsonObject.has("int_key");
boolean b3 = jsonObject.has("dummy_key");
                            
show output in Log
Log.e("value1", b1 + "");
Log.e("value2", b2 + "");
Log.e("value3", b3 + "");
                            
Output
************************************
true
true
false
************************************

2. isNull("key_name");

Syntax
jsonObject.isNull("object_key_name");
it used for check specific OBJECT exists or not, if exists it checks specific object is NULL or not
if key not exists or exists but equals NULL it returns TRUE otherwise it returns FALSE.

Example
boolean b1 = jsonObject.isNull("dummy_object_key");
boolean b2 = jsonObject.isNull("json_object_key");
boolean b3 = jsonObject.isNull("json_array_key");

show output in Log
Log.e("value1", b1 + "");
Log.e("value2", b2 + "");
Log.e("value3", b3 + "");

Output
************************************
true
false
false
************************************

3. keys();

Syntax
jsonObject.keys();
Used for get JSONArray value from JSON;

Ex
Iterator<String> jsonKeys = jsonObject.keys();

show output in Log
int i = 0;
while (jsonKeys.hasNext()) {
Log.e("Value", "Keys : " + i + " : " + jsonKeys.next());
                                      i++;
}

Output
************************************
Keys : 0 : boolean_key
Keys : 1 : int_key
Keys : 2 : long_key
Keys : 3 : double_key
Keys : 4 : string_key
Keys : 5 : json_object_key
Keys : 6 : json_array_key
************************************
    
4. getBoolean("key_name");
         
Syntax
jsonObject.getBoolean("boolean_key_name");
Used for get boolean value from JSON.

Ex
boolean b = jsonObject.getBoolean("boolean_key");

show output in Log
Log.e("Value", "boolean : " + b);

Output
************************************
boolean : true
************************************

5. getInt("key_name");

Syntax
jsonObject.getInt("int_key_name");
Used for get Integer value from JSON;

Ex
int i = jsonObject.getInt("int_key");

show output in Log
Log.e("Value", "int : " + i);

Output
************************************
int : 42798
************************************

6. getLong("key_name");

Syntax
jsonObject.getLong("long_key_name");
Used for get Long value from JSON;

Ex
long l = jsonObject.getInt("long_key");

show output in Log
Log.e("Value", "long : " + l);

Output
************************************
long : 5349
************************************

7. getDouble("key_name");

Syntax
jsonObject.getDouble("double_key_name");
Used for get Double value from JSON;

Ex
double d = jsonObject.getInt("double_key");

show output in Log
Log.e("Value", "double : " + d);

Output
************************************
double : 70.0
************************************

8. getString("key_name");

Syntax
jsonObject.getString("string_key_name");
Used for get String value from JSON;

Ex
String s = jsonObject.getString("string_key");

show output in Log
Log.e("Value", "String : " + s);

Output
************************************
String : ganesh
************************************

9.get("key_name");

Syntax
jsonObject.get("object_key_name");
Used for get Object value from JSON;

Ex
Object o1 = jsonObject.get("json_object_key");
Object o2 = jsonObject.get("json_array_key");

show output in Log
Log.e("Value", "Object : " + o1);
Log.e("Value", "Object : " + o2);

Output
************************************
Object1 : {"name":"venkat","phno":"0123456789","city":"Hyd"}
Object2 : [50,60,70,80]
************************************

10. getJSONObject("key_name");

Syntax
jsonObject.getJSONObject("json_object_key_name");
Used for get JSONObject value from JSON;

Ex
JSONObject jsonObject1 = jsonObject
                                                .getJSONObject("json_object_key");

show output in Log
Log.e("Value", "JSONObject : " + jsonObject1);

Output
************************************
JSONObject : {"name":"venkat","phno":"0123456789","city":"Hyd"}
************************************

11. getJSONObject("key_name");

Syntax
jsonObject.getJSONObject("json_array_key_name");
Used for get JSONArray value from JSON;

Ex
JSONArray jsonArray = jsonObject.getJSONArray("json_array_key");

show output in Log
Log.e("Value", "JSONArray : " + jsonArray);

Output
************************************
JSONArray : [50,60,70,80]
************************************

************************************

    Download Full Source Code HERE

************************************

Monday, 29 May 2017

Android Device WiFi Turn ON and OFF Programatically

If you want access device wifi programatically we need
  1. Permissions
  2. WifiManager

1. In Project Manifest file add thease permissons
<!-- we try to Enable or Disable WIFI, so we need permission -->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
   
<!-- we try to fetching current WIFI state, so we need permission -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />


2. In Project Activity file add this code to get WifiManager
getting WIFI MANAGER from System Service
WifiManager  wifiManager = (WifiManager)          
                             getSystemService(Context.WIFI_SERVICE);


methods to access wifi status and change wifi status

fetching current WIFI status
wifiManager.isWifiEnabled()

change wifi status
wifiManager.setWifiEnabled(true or false);

enable wifi status
wifiManager.setWifiEnabled(true);

disable wifi status
wifiManager.setWifiEnabled(false);

-------------------------------------------------------------
Screen Shots:

-------------------------------------------------------------

1. MainActivity
public class MainActivity extends Activity {

          WifiManager wifiManager;
          Button buttonON, buttonOFF;
          TextView textView;

          @Override
          protected void onCreate(Bundle savedInstanceState) {
                   super.onCreate(savedInstanceState);
                   setContentView(R.layout.activity_main);

                   // calling references or linking xml to java
                   textView = (TextView) findViewById(R.id.textView);
                   buttonON = (Button) findViewById(R.id.buttonON);
                   buttonOFF = (Button) findViewById(R.id.buttonOFF);

                   // getting WIFI MANAGER from System Service
                   wifiManager = (WifiManager)            
                             getSystemService(Context.WIFI_SERVICE);

                   // fetching current WIFI status and update to TextView.
                   if (wifiManager.isWifiEnabled())
                             textView.setText("WIFI : ENABLED");
                   else
                             textView.setText("WIFI : DISABLED");

                   // listener for ON button
                   buttonON.setOnClickListener(new OnClickListener() {

                             @Override
                             public void onClick(View v) {
                                      // enable WIFI
                                      wifiManager.setWifiEnabled(true);
                                      textView.setText("WIFI : ENABLED");
                             }
                   });

                   // listener for OFF button
                   buttonOFF.setOnClickListener(new OnClickListener() {

                             @Override
                             public void onClick(View v) {
                                      // disable WIFI
                                      wifiManager.setWifiEnabled(false);
                                      textView.setText("WIFI : DISABLED");
                             }
                   });

          }

}

2. activity_main.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"
    android:background="#800000ff"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.venky.wifienabledemo.MainActivity" >

    <Button
        android:id="@+id/buttonON"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="121dp"
        android:text="ON"
        android:textSize="15dp" />

    <Button
        android:id="@+id/buttonOFF"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/buttonON"
        android:layout_below="@+id/buttonON"
        android:layout_marginTop="76dp"
        android:layout_weight="1"
        android:text="OFF"
        android:textSize="15dp" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:gravity="center"
        android:text="WIFI : "
        android:textColor="#ffffff"
        android:textSize="25dp"
        android:textStyle="bold" />

</RelativeLayout>

3. AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.venky.wifienabledemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <!-- we try to Enable or Disable WIFI, so we need permission -->
    <uses-permission       
        android:name="android.permission.CHANGE_WIFI_STATE" />
    <!-- we try to fetching current WIFI state, so we need permission -->
    <uses-permission       
       android:name="android.permission.ACCESS_WIFI_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER"             />
            </intent-filter>
        </activity>
    </application>

</manifest>

************************************

    Download Full Source Code HERE

************************************