Wednesday, 3 August 2016

JSON Parsing | Insert Data in Jason Format in Mobile internal memory | Get or Parsing JSON data from mobile internal memory

Code verified on :- Android studio 2.1.2

Step 1:- Open Android Studio.
Step 2:-Click on File > New > New Project.
Step 3:- Open activity_main.xml file and take 2 EditText and 2 Button , write following code.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"   >

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:hint="Employee Name"
        android:id="@+id/editName"
        android:inputType="text"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:hint="Employee Salary"
        android:id="@+id/editSalary"
        android:inputType="number"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="40dp">
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Insert"
            android:onClick="Insert"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Get"
            android:onClick="Get"/>
    </LinearLayout>

</LinearLayout>

Step 4:- Now Open your MainActivity.java file and write following code...

package androidhubb.jasonparsing;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.FileReader;
import java.io.FileWriter;

public class MainActivity extends AppCompatActivity{
    //object..    
EditText edtName,edtSalary;
    @Override    
protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edtName=(EditText)findViewById(R.id.editName);
        edtSalary=(EditText)findViewById(R.id.editSalary);
    }

    //Insert data in JSON format... 
  
 public void Insert(View v){
        JSONObject obj=new JSONObject();
        JSONArray arr=new JSONArray();
        JSONObject obj_ind=new JSONObject();
        try {
            obj_ind.put("NAME", edtName.getText().toString().trim());
            obj_ind.put("SALARY", edtSalary.getText().toString().trim());

            arr.put(obj_ind);
            obj.put("Employee",arr);

            FileWriter f=new FileWriter("/storage/sdcard0/wer.txt");
            f.write(obj.toString());
            f.flush();
            f.close();
            edtName.setText("");
            edtSalary.setText("");
        }
        catch (Exception e){}
    }

    //get..  
  
public void Get(View v){
        try {
            FileReader f = new FileReader("/storage/sdcard0/wer.txt");
            int i=f.read();
            String str="";
            String name="",salary="";
            while(i!=-1){
                str=str+(char)i;
                i=f.read();
            }
            JSONObject obj=new JSONObject(str);
            JSONArray arr=obj.getJSONArray("Employee");
            for(int j=0;j<arr.length();j++){
                JSONObject obj_ind=arr.getJSONObject(j);
                name=obj_ind.getString("NAME");
                salary=obj_ind.getString("SALARY");
                Toast.makeText(getApplicationContext(),name+"\n"+salary,Toast.LENGTH_LONG).show();
            }
        }
        catch (Exception e){}
    }

}

Step 1:- Open manifest.xml file anf take following 2 permission.

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"   
 package="androidhubb.jasonparsing">
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

    </manifest>



Output:-







1 comment:

  1. This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic.
    Android Training institute in chennai with placement | Best Android Training in velachery

    ReplyDelete