Thursday 12 May 2016

JSON parsing using Volley

Android JSON parsing using Volley
What is Android Volley? 


Android Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster.


Creating New Project.

    Open android studio and create a new project.
     File => New => New Project => Configure your new project => Select the form factor yours app will run on => Add an Activity to Mobile => Customize the Activity => Finish.

    First we need to add Volley Library to our project.   
    compile 'com.mcxiaoke.volley:library-aar:1.0.0'

     Create Xml file in project.     
    Open => app => res => layout - activity_main.xml.

<?xml version="1.0" encoding="utf-8"?>
<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">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="24dp"
        android:layout_marginRight="24dp"
        android:gravity="center"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/linearLayput"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/imageView"
            android:orientation="vertical">

            <EditText
                android:id="@+id/email"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:drawableRight="@drawable/user"
                android:focusableInTouchMode="true"
                android:hint="EmailAddress"
                android:inputType="textEmailAddress"
                android:paddingLeft="8dp"
                android:singleLine="true" />

            <EditText
                android:id="@+id/pwd"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:layout_marginTop="8dp"
                android:drawableRight="@drawable/password"
                android:hint="Password"
                android:inputType="textPassword"
                android:paddingLeft="8dp"
                android:singleLine="true" />
        </LinearLayout>

        <Button
            android:id="@+id/login"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/linearLayput"
            android:layout_marginTop="16dp"
            android:background="@color/primary"
            android:text="Sign In Now"
            android:textColor="@android:color/white" />
    </RelativeLayout>
</LinearLayout>


 Create the Java file in project.
 Open app => main => src = LoginActivity.java

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.digi.soft.Utils.Credentials;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Laptop on 12-05-2016.
 */

public class LoginActivity extends AppCompatActivity {
    EditText emailEditText, pwdEditText;
    Button loginButton;
    ProgressDialog dialog;
    public static final String LOGIN_URL = "Please enter the login API";
    public static final String KEY_EMAIL = "Enter email parameter";
    public static final String KEY_PWD = "Enter password parameter";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activtiy_login);
        Init();
        Listner();
    }

    private void Listner() {
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (emailEditText.length() == 0) {
                    Snackbar.make(v, "Enter user email.", Snackbar.LENGTH_LONG).setAction("Action", null).show();
                } else if (pwdEditText.length() == 0) {
                    Snackbar.make(v, "Enter user password.", Snackbar.LENGTH_LONG).setAction("Action", null).show();
                } else {
                    LoginUser();
                }
            }
        });
    }

    private void LoginUser() {

        final String email = emailEditText.getText().toString().trim();
        final String pwd = pwdEditText.getText().toString().trim();

        StringRequest stringRequest = new StringRequest(Request.Method.POST, LOGIN_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        showDialog(false);
                        Toast.makeText(LoginActivty.this, response, Toast.LENGTH_LONG).show();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(LoginActivty.this, error.toString(), Toast.LENGTH_LONG).show();
                    }
                }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put(KEY_EMAIL, email);
                map.put(KEY_PWD, pwd);
                return map;
            }
        };
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }

    private void showDialog(boolean check) {
        if (check) {
            dialog = ProgressDialog.show(LoginActivity.this, "", "Loading.....");
            dialog.show();
        } else {
            dialog.dismiss();
        }
    }

    private void openProfile() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }

    private void Init() {
        emailEditText = (EditText) findViewById(R.id.email);
        pwdEditText = (EditText) findViewById(R.id.pwd);
        loginButton = (Button) findViewById(R.id.login);
    }

   
 Add Internet permission in your manifest
 <uses-permission android:name="android.permission.INTERNET"/>


May this code help you. Thanks!!!!!!

No comments:

Post a Comment