Thursday, 12 October 2017

Android Volley Get JSON Data

 

1) Import Dependencies

 The easiest way to add Volley to your project is to add the following dependency to your app's build.gradle file:

dependencies {
    ...
    compile 'com.android.volley:volley:1.0.0'
}

2) Add Internet Permission On Manifest File 

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

3) Main Activity Design

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="com.example.ismails.myapplication.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="JSON Data Here"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="104dp"
        android:id="@+id/jsonData"
        android:textColor="#000000"
        android:textStyle="bold"
        android:textSize="20dp"
        android:textAlignment="center"/>

    <Button
        android:id="@+id/btn_json"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="79dp"
        android:text="Get JSON Data" />


</RelativeLayout>


3) M
ain Activity Java

public class MainActivity extends AppCompatActivity {

    // Todo : Will show the string "data" that holds the results
    TextView results;
    // Todo :  URL of object to be parsed
    String JsonURL = "http://jsonplaceholder.typicode.com/todos/1";

    // Todo : Defining the Volley request queue that handles the URL request concurrently
    RequestQueue requestQueue;

    Button btn;

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

        // Todo :  Creates the Volley request queue

        requestQueue = Volley.newRequestQueue(this);

        // Todo : Casts results into the TextView And Button found within the main layout XML with id jsonData

        results = (TextView) findViewById(R.id.jsonData);
        btn = (Button)findViewById(R.id.btn_json);

            /*Todo : Set OnClick To Get JSON Data In TextView*/

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Todo : Creating the JsonObjectRequest class called obreq, passing required parameters:
                // Todo : GET is used to fetch data from the server, JsonURL is the URL to be fetched from.

                JsonObjectRequest jsObjRequest = new JsonObjectRequest
                        (Request.Method.GET, JsonURL, null,

                                new Response.Listener<JSONObject>() {

                            @Override
                            public void onResponse(JSONObject response) {

                                /*Todo : Assign Json Value In TextView*/

                                results.setText("JSON Data Is \n: " + response.toString());
                            }
                        }, new Response.ErrorListener() {

                            @Override
                            public void onErrorResponse(VolleyError error) {
                                // TODO Auto-generated method stub

                            }
                        });

                // Todo : Adds the JSON object request "jsObjRequest" to the request queue

                requestQueue.add(jsObjRequest);
            }
        });


    }

}


Android Studio - Get Current Latitude And Longitude

1. Add this dependencies in your gradle file     implementation 'com.google.android.gms:play-services-location:18.0.0' 2. Add this p...