sushildlh sushildlh - 6 months ago 79
Android Question

Rerofit + Firebase

How we connect Retrofit with Firebase for consumation of RESTAPI ?

Is it possible that without any back-end we can implement RESTAPI ?

Thank you guys.........................

Answer

Retrofit + Firebase

Data store in JSON file in the Firebase.

We can change JSON data direct also like create JSON file and import to the Firebase directly or we export it to the disk and create static JSON file .

It is easy way to implement RESTAPI without ANY BECK-END knowledge. That is helpful for the beginner .

We need a Firebase account and some basic knowledge of RESTApi.

First go to Firebase and create your account.

After creating your account go to AppCreation and create your App.

App

After creation of App it automatically create URL like this https://myapp-sushildlh.firebaseio.com, which we can use in the RESTApi Calls.

We have to add this dependencies in GRADLE.

compile 'com.squareup.retrofit2:retrofit:2.0.2'//this for the retrofit 
compile 'com.squareup.retrofit2:converter-gson:2.0.0'// this for the JSON converter

Know About the code ............

We have to create API interface which have all RESTApi methods

public interface Api {
    @POST("/upload/{new}.json")
    Call<User> setData(@Path("new") String s1, @Body User user);

    @GET("/upload/sushil.json")
    Call<User> getData();

    @PUT("/upload/{new}.json")
    Call<User> setDataWithoutRandomness(@Path("new") String s1, @Body User user);

}

POJO for the JSON data which is used for converting data into JSON or OBJECT or Vice-Versa ..... like that

public class User {

    String name;
    String address;

    public User(String name, String address) {
        this.address = address;
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

Know the RESTApi calls and uses.....

Know we create the Retrofit Object like this.....

 Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://myapp-sushildlh.firebaseio.com")//url of firebase app
                .addConverterFactory(GsonConverterFactory.create())//use for convert JSON file into object
                .build(); 

Know we create Api Object like this...

 Api api = retrofit.create(Api.class);//use of interface 

there is call for PUT method....

Call<User> call1=api.setDataWithoutRandomness("sushil", new User("sushil", "mumbai"));
        call1.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                t1.setText("Success "+response.body().getName());
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {
                t1.setText("fail");
            }
        });

there is call for POST method....

Call<User> call = api.setData("mahesh", new User("mahesh", "delhi"));
    call.enqueue(new Callback<User>() {
        @Override
        public void onResponse(Call<User> call, Response<User> response) {
            t1.setText("Success");
        }

        @Override
        public void onFailure(Call<User> call, Throwable t) {
            Log.d("sam", "fail");
            t1.setText("fail");
        }
    });

there is call for GET method....

 Call<User> call2=api.getData();
        call2.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                t1.setText("success "+response.body().getName()+" "+response.body().getAddress());
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {
                t1.setText("fail");
            }
        });

and finally json file which is created on the Firebase.

{
  "upload" : {
    "mahesh" : {
      "-KIBTSbkyEGFgX_VqWJQ" : {
        "address" : "delhi",
        "name" : "mahesh"
      }
    },
    "sushil" : {
      "address" : "mumbai",
      "name" : "sushil"
    }
  }
}

In the Json file "-KIBTSbkyEGFgX_VqWJQ" this is Anonymously Name which is generated when we use POST method .

That is all....

Enjoy coding......