Tuesday, July 30, 2019

Volley for loading data

Volley is a REST  network library which is  used to send and get data from the server we need to do json parsing to fetch data from the server below s the example for the same.


User permission

<uses-permission android:name="android.permission.INTERNET" />

Dependencies

implementation 'com.android.volley:volley:1.1.0'

Java class

package com.vishalsingh.practice;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

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.squareup.picasso.Picasso;

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

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

public class VolleyActivity extends AppCompatActivity {
    TextView textView;
    Button button;
    String url="your url ";
    ImageView imageView;
    String Imageurl;

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

        textView=findViewById(R.id.textview);
        button=findViewById(R.id.button);
        imageView=findViewById(R.id.Imageview);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringRequest sr=new StringRequest(1, url, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jo=new JSONObject(response);
                            JSONArray jo1=jo.getJSONArray("Response");
                            JSONObject jo2=jo1.getJSONObject(0);
                            String data1=jo2.getString("Name");
                            Imageurl=jo2.getString("Image");
                            Picasso.get().load(Imageurl).into(imageView);
                            textView.setText(data1+"\n");



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

                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                    }
                }){
                    @Override
                    public   Map<String,String> getParams(){
                        Map<String, String> params = new HashMap<>();
                        params.put("UserId", "0d770e2b-eeff-4652-8282-5c756941d280");
                        return params;
                }};
                RequestQueue rq= Volley.newRequestQueue(VolleyActivity.this);
                rq.add(sr);
            }
        });


    }
}

Xml file

<?xml version="1.0" encoding="utf-8"?>
<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=".VolleyActivity">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:text="fetch"
        app:layout_constraintTop_toTopOf="parent"/>
    <ImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:id="@+id/Imageview"

        app:layout_constraintTop_toBottomOf="@+id/button"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textview"
        app:layout_constraintTop_toBottomOf="@+id/Imageview"/>

</android.support.constraint.ConstraintLayout>

Sample Json

{
"Status":true,
"Error":
{
"Code":0,
"Message":""
},
"Response":
[{"Name":"Shubham Shrivastava",
"Image":"http://103.115.194.111:906/Readers/uploads/Screenshot_20190424-202039.jpg",
"Gender":"Mr.",
"DateOfBirth":"26 Dec 2018",
"Address":"Noida",
"Country":"INDIA",
"PinCode":"485001",
"EmailId":"shri.shubham86@gmail.com",
"MobileNo":"9074762379",
"isPartner":"No",
"City":"",
"State":"Madhya Pradesh",
"Booth":"Other",
"Constituency":"Satna",
"Assembly":"Satna",
"Citizenship":null}
]
}

Alert Dialog Box

Alert Dialog Box Android AlertDialog   can be used to display the dialog message with OK and Cancel buttons. It can be used to inter...