Saturday, 19 December 2020

Retrofit2 in android


Dependencies 

implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.6.1'

// additional things
implementation 'com.squareup.okhttp3:logging-interceptor:4.0.1'
implementation 'com.squareup.okhttp3:okhttp:4.0.1'

Need to create classes
ApiInterface.java
public interface ApiInterface {


@FormUrlEncoded
@POST("get-token")
Call<ResponseBody> getToken(@FieldMap Map<String, String> fieldMap);

}

ApiClient.java
public class ApiClient {

private static Retrofit retrofit;
private static Retrofit retrofitPlaid;

public static Retrofit getClient() {

HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(
interceptor)
.readTimeout(
60, TimeUnit.MINUTES)
.writeTimeout(
60, TimeUnit.MINUTES)
.build();


retrofit = new Retrofit.Builder()
.baseUrl(
"service url")
.addConverterFactory(
GsonConverterFactory.create())
.client(
client)
.build();


return retrofit;
}

}

Implement in Activity or Service
# Create apiinterface object
ApiInterface apiService;
apiService = ApiClient.getClient().create(ApiInterface.class);

#call api (Normal)
Map<String, String> map = new HashMap<String, String>();
map.put("request_param_name", "1");
Call<ResponseBody> call= apiService.dashboard(AppSharedPreferences.getSharePreference(mContext).getAccessToken(), map);

#Response
  call.clone().enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

try {

if (response.isSuccessful()) {

if (response.body().getSuccess() == 1) {
Toast.makeText(mContext, response.body().getData().getMessage(), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(mContext, response.body().getError().get(0), Toast.LENGTH_SHORT).show();
}
} else {
assert response.body() != null;
Toast.makeText(mContext, response.body().getError().get(0), Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
}
});
}

#call api (Image with text)
@Multipart
@POST("users/edit_profile")
Call<ResponseBody> editProfile(@PartMap Map<String, RequestBody> fieldMap, @Part MultipartBody.Part image);

// creaet param
MultipartBody.Part body = null;
if (imageFile != null) {
RequestBody reqFile = RequestBody.create(MediaType.parse("multipart/form-data"), imageFile);
body = MultipartBody.Part.createFormData("image", imageFile.getName(), reqFile);
}

Map<String, RequestBody> map = new HashMap<>();
map.put(Constant.TAG_USER_ID, toRequestBody(user.user_id));


Call<ResponseBody> callApi = apiService.editProfile(map, body);
editProfileHttpUrl = callApi.request().url();
callApi.clone().enqueue(this);

public static RequestBody toRequestBody(String value) {
RequestBody body = RequestBody.create(MediaType.parse("text/plain"), value);
return body;
}

#call api (only Image)
@Multipart
@POST("edit-profile")
Call<UserData> uploadProfile(@Header("Authorization") String auth, @Part MultipartBody.Part image);

Request
MultipartBody.Part body = null;
if (imageFile != null) {
RequestBody reqFile = RequestBody.create(MediaType.parse("multipart/form-data"), imageFile);
body = MultipartBody.Part.createFormData("profile_pic", imageFile.getName(), reqFile);
}
Call<UserData> callUploadProfile = apiService.uploadProfile(AppSharedPreferences.getSharePreference(mContext).getAccessToken(), body);

Monday, 3 February 2020

Location foregroud service android native

- This is a location update demo
- Frequently update location
- When the app in the background then starts the foreground s service to get the location.
- It even gets a location to continue if the app finished from the background.


Download Demo: https://drive.google.com/open?id=1Hghm--12etkZq-e1NV9R8O6_XygBBbKL

Monday, 20 January 2020

CashFree Payment gateway demo/Example


just comment or uncomment for Test and Production mode


Download demo here:

https://drive.google.com/open?id=1XdK3xhKf8Ro4oDNoVrzcVJ9TW5QRfnSy

Run/install/debug Android applications over Wi-Fi ?

Open Teminal/cmd --------------- Below steps is for Android 10 or lower Step 1 - Connect the device via USB and make sure debugging is work...