Monday, 16 July 2018

JSON PARSING

 

Android working with and Recycler View And JSON PARSING API IMPLEMENT Process.





package xyz;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
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.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;

public class Json_Obect extends AppCompatActivity {
    private Context mContext;
    RecyclerView my_list;
    List_Adapter gridViewAdapter;

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

        mContext = Json_Obect.this;

        initiView();
        Get_CardApi();

    }

    private void initiView() {
        my_list = findViewById(R.id.my_list);
    }

    private void Get_CardApi() {

        String Url = "xyz";
        // showProgressDialog("Please wait...");        if (NetworkUtil.isNetworkAvailable(mContext)) {
            // POST parameters            Map<String, String> params = new HashMap<String, String>();

           /* params.put("dsaCode", dsa_number);            JSONObject jsonObj = new JSONObject(params);            // Request a json response from the provided URL            JsonObjectRequest jsonObjRequest = new JsonObjectRequest(Request.Method.POST, Url, jsonObj, new Response.Listener<JSONObject>() {*/
            // Request a json response from the provided URL            JsonObjectRequest jsonObjRequest = new JsonObjectRequest(Request.Method.GET, Url, new Response.Listener<JSONObject>() {
                @Override                public void onResponse(JSONObject response) {
                    Toast.makeText(getApplicationContext(), response.toString(), Toast.LENGTH_SHORT).show();

                    String msg = response.toString();
                    //  Utils.showToast(mContext, msg);                    JSONObject json = response;
                    System.out.println(response);
                    if (json.has("contacts")) {
                        try {
                            JSONArray data = json.getJSONArray("contacts");
                            HashMap<String, String> list = new HashMap<String, String>();
                            //removeProgressDialog();
                            if (data.length() > 0) {
                                gridViewAdapter = new List_Adapter(mContext, data);
                                RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
                                // RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getApplicationContext(), 2);                                my_list.setLayoutManager(mLayoutManager);
                                // my_list.setItemAnimator(new DefaultItemAnimator());                                my_list.setAdapter(gridViewAdapter);
                            } else {
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();

                        }
                    }
                }
            },
                    new Response.ErrorListener() {
                        @Override                        public void onErrorResponse(VolleyError error) {
                            //removeProgressDialog();                            {
                                Toast.makeText(mContext, "Api Error, Please try again " + error.getMessage(), Toast.LENGTH_SHORT).show();

                            }
                        }

                    }) {
                @Override                public Map<String, String> getHeaders() throws AuthFailureError {
                    Map<String, String> params = new HashMap<String, String>();
                   /* String Token = new GetProfileDetails(mContext).getToken();                    String auth = "Bearer " + Token;                    params.put("Authorization", auth);*/                    return params;
                }
            };
            // Add the request to the RequestQueue.            RequestQueue requestQueue = Volley.newRequestQueue(this);
            requestQueue.add(jsonObjRequest);

        }
    }
}








package XYZ;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;

public class List_Adapter extends RecyclerView.Adapter<List_Adapter.MyViewHolder> {
    private JSONArray kyc_List;
    private Context mContext;

    public List_Adapter(Context mcontext, JSONArray kycList) {
        this.mContext = mcontext;
        this.kyc_List = kycList;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        TextView name, email, mobile;


        public MyViewHolder(final View v) {
            super(v);

            name = v.findViewById(R.id.name);
            email = v.findViewById(R.id.email);
            mobile = v.findViewById(R.id.mobile);

        }
    }

    @Override    public List_Adapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.listview, parent, false);

        return new List_Adapter.MyViewHolder(itemView);
    }

    @Override    public void onBindViewHolder(final List_Adapter.MyViewHolder holder, final int position) {
        try {
            final RecyclerView.ViewHolder holder1 = holder;
            kyc_List.getJSONObject(position);
            //String text_Name = kyc_List.getJSONObject(position).optString("name").toString();            String text_Email = kyc_List.getJSONObject(position).optString("email").toString();
            String text_Mobile = kyc_List.getJSONObject(position).optString("address").toString();

            holder.name.setText("Andrew PJ");
            holder.email.setText(text_Email);
            holder.mobile.setText(text_Mobile);

            //  holder.text_des.setText(kyc_List.getJSONObject(position).optString("descriptions").toString());
           /* if (!Imgurl.equals("") && Imgurl != null) {
                Utils.loadImage(holder.productIcon, Imgurl, mContext, R.drawable.ic_profile_img);            }*/

        } catch (JSONException e) {
            e.printStackTrace();

        }

    }


    @Override    public int getItemCount() {
        return kyc_List.length();
    }

}



package XYZ;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;


public class NetworkUtil {

    public static boolean isNetworkAvailable(Context context) {
        boolean value = false;


        ConnectivityManager manager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = manager.getActiveNetworkInfo();
        //       /* NetworkInfo networkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        boolean isWifiConn = networkInfo.isConnected();        networkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);        boolean isMobileConn = networkInfo.isConnected();*/        //        if (info != null && info.isAvailable()) {
            value = true;
            Log.d("Is network available", String.valueOf(value));

        } else {
          

        }

        return value;
    }

}