Wednesday, 21 December 2016

Retrofit in Android


Retrofit is a type-safe REST client for Android developed by Square. The library provides a powerful framework for authenticating and interacting with APIs and sending network requests with OkHttp

Integrate Retrofit in Android follow Below steps:

1) Add dependencies in build.gradle file



compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
compile 'com.squareup.retrofit2:converter-scalars:2.1.0'

2) Create Interface which will have all the api calls


public interface DataCall{


    //Get Method Call
    @GET("Pass your api path) //ex: "register.php"
    Call<String>getDataGet(@Query("name") String name);


   //Post Method Call
@FormUrlEncoded @POST("index.php") Call<String>getDataPost(@Field("name")String name);
   //Post Method Call with Query annotation
@POST("index.php") Call<String>getDataPostQuery(@Query("name")String name); //Post Method Call with HashMap request
    @POST("index.php")
    Call<String>getDataPostQueryMap(@QueryMap HashMap<String,String> map);

   //Post Method Call with Json request
    @POST("index.php")
    Call<String>getDataPostQueryJson(@Body RequestBody name);

   
}

3) Create Retrofit object and call api methods.

Retrofit retrofit = new Retrofit.Builder().baseUrl("pass here your base url, 
shoul end with /")
        .addConverterFactory(ScalarsConverterFactory.create()).build();
//ScalarsConverterFactory.create() is for String response handling
//GsonConverterFactory.create() is used for automatically will parse the 
//response with proper bean classes


DataCall datacall = retrofit.create(DataCall.class);


//Get Method call
 Call<String> call = datacall.getDataGet("GET name");
     call.enqueue(new Callback<String>() {
         @Override         public void onResponse(Call<String> call,
 Response<String> response) {
             System.out.println("Response data :"+response.body());
         }

         @Override         public void onFailure(Call<String> call, Throwable t) {
             System.out.println("Response data :"+t.getMessage());
         }
     });

//Post Method call
call = datacall.getDataPost("POST name"); call.enqueue(new Callback<String>() { @Override public void onResponse(Call<String> call,
Response<String> response) {
             System.out.println("Response data :"+response.body());
         }

         @Override         public void onFailure(Call<String> call, Throwable t) {
             System.out.println("Response data :"+t.getMessage());
         }
     });


//Post Method call
call = datacall.getDataPostQuery("POST Query"); call.enqueue(new Callback<String>() { @Override public void onResponse(Call<String> call,
Response<String> response) {
             System.out.println("Response data :"+response.body());
         }

         @Override         public void onFailure(Call<String> call, Throwable t) {
             System.out.println("Response data :"+t.getMessage());
         }
     });



//Post Method call with Hash map request
HashMap<String,String>map=new HashMap(); JSONObject ob2=null; // JSONObject map=new JSONObject(); try { map.put("name1", "Map name"); map.put("password1", "Map password"); ob2=new JSONObject(map); }catch (Exception e) { e.printStackTrace();; } RequestBody body = RequestBody.
create(okhttp3.MediaType.parse("application/json; charset=utf-8"),ob2.toString());

        call = datacall.getDataPostQueryJson(body);
     call.enqueue(new Callback<String>() {
         @Override         public void onResponse(Call<String> call, 
Response<String> response) {
             System.out.println("Response data :"+response.body());
         }

         @Override         public void onFailure(Call<String> call, Throwable t) {
             System.out.println("Response data :"+t.getMessage());
         }
     });

//Post Method call with JSon Request

  JSONObject map=new JSONObject();
     try {
         map.put("name1", "Map name");
         map.put("password1", "Map password");

          
     }catch (Exception e)
     {
         e.printStackTrace();;
     }
     RequestBody body = RequestBody.
create(okhttp3.MediaType.parse("application/json; charset=utf-8"),map.toString());

        call = datacall.getDataPostQueryJson(body);
     call.enqueue(new Callback<String>() {
         @Override         public void onResponse(Call<String> call, 
Response<String> response) {
             System.out.println("Response data :"+response.body());
         }

         @Override         public void onFailure(Call<String> call, Throwable t) {
             System.out.println("Response data :"+t.getMessage());
         }
     });