JSON Parsing Using Retrofit

What is Retrofit?

Retrofit is Rest Client used for Android to upload and retrieve JSON. It is developed by Square.

Step 1

For using retrofit in your app we need to add a library dependency in Module:app gradle.
compile 'com.squareup.retrofit:retrofit:1.9.0'

Below is the example url that is needed to be parsed.

http://dummyjson.890m.com/getcontacts.php

In Retrofit we just break the url in parts we need to exclude the root url in our case it is http://dummyjson.890m.com/ and second part of the url is /getcontacts.php which we need to pass in the interface.

Step 2

Give internet permmission in the manifest
<uses-permission android:name="android.permission.INTERNET"/>

Step 3

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"
android:orientation="vertical"
tools:context="eazy.mysqliteblog.MainActivity">

<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/data"></ListView>

</LinearLayout>


Step 4

Create an interface where we need to pass the second part of the url.

DummyApi.java
import retrofit.Callback;
import retrofit.client.Response;
import retrofit.http.GET;

/**
* Created by Administrator on 07-06-2017.
*/

public interface DummyApi {

@GET("/getcontacts.php")
public void getAllData(Callback<Response> responseCallback);
}


Step 5

MainActivity.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import retrofit.Callback;
import retrofit.RestAdapter;
import retrofit.RetrofitError;
import retrofit.client.Response;

public class MainActivity extends AppCompatActivity {


ListView listView;
ArrayList al=new ArrayList();
StringBuilder stringBuilder=new StringBuilder();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

listView= (ListView) findViewById(R.id.data);


RestAdapter restAdapter=new RestAdapter.Builder().setEndpoint("http://dummyjson.890m.com/").build();
DummyApi dummyApi=restAdapter.create(DummyApi.class);
dummyApi.getAllData(new Callback<Response>() {
@Override
public void success(Response response, Response response2) {

try {
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(response.getBody().in()));
String line;
while ((line= bufferedReader.readLine())!=null){
stringBuilder.append(line);

}

JSONArray jsonArray=new JSONArray(String.valueOf(stringBuilder));
for (int i=0;i<jsonArray.length();i++){
JSONObject jsonObject=jsonArray.getJSONObject(i);
String name=jsonObject.getString("name");
String phone=jsonObject.getString("phone");
al.add(name+" "+phone);




}
listView.setAdapter(new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,al));



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


}

@Override
public void failure(RetrofitError error) {

}
});
}

}

img3

Comments

  1. Easy to understand....Thank you...

    ReplyDelete
  2. wooooow... Nice Work ...easy to understand. i hope u will update more practicals...
    i will waiting for more practicals...

    Plz upload Google map related practicals

    ReplyDelete
  3. Aakash S. Raval9 June 2017 at 07:50

    Thank you Ma'am for this program, it is really helpful to me.

    ReplyDelete

Post a Comment