Java tutorial
/** * Copyright (C) 2015 Fernando Cejas Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.howareyoudoing.data.net; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import com.howareyoudoing.data.entity.SingleUser; import com.howareyoudoing.data.entity.mapper.UserEntityJsonMapper; import com.howareyoudoing.data.exception.NetworkConnectionException; import com.squareup.okhttp.MediaType; import com.squareup.okhttp.RequestBody; import java.net.MalformedURLException; import java.util.List; import io.reactivex.Observable; /** * {@link RestApi} implementation for retrieving data from the network. */ public class RestApiImpl implements RestApi { private final Context context; private final UserEntityJsonMapper userEntityJsonMapper; /** * Constructor of the class * * @param context {@link android.content.Context}. * @param userEntityJsonMapper {@link UserEntityJsonMapper}. */ public RestApiImpl(Context context, UserEntityJsonMapper userEntityJsonMapper) { if (context == null || userEntityJsonMapper == null) { throw new IllegalArgumentException("The constructor parameters cannot be null!!!"); } this.context = context.getApplicationContext(); this.userEntityJsonMapper = userEntityJsonMapper; } @Override public Observable<List<SingleUser>> userEntityList() { return Observable.create(emitter -> { if (isThereInternetConnection()) { try { String responseUserEntities = getUserEntitiesFromApi(); if (responseUserEntities != null) { emitter.onNext(userEntityJsonMapper.transformUserEntityCollection(responseUserEntities)); emitter.onComplete(); } else { emitter.onError(new NetworkConnectionException()); } } catch (Exception e) { emitter.onError(new NetworkConnectionException(e.getCause())); } } else { emitter.onError(new NetworkConnectionException()); } }); } @Override public Observable<SingleUser> userEntityById(final int userId) { return Observable.create(emitter -> { if (isThereInternetConnection()) { try { String responseUserDetails = getUserDetailsFromApi(userId); if (responseUserDetails != null) { // emitter.onNext(userEntityJsonMapper.transformUserEntity(responseUserDetails)); emitter.onComplete(); } else { emitter.onError(new NetworkConnectionException()); } } catch (Exception e) { emitter.onError(new NetworkConnectionException(e.getCause())); } } else { emitter.onError(new NetworkConnectionException()); } }); } private String getUserEntitiesFromApi() throws MalformedURLException { String s = "{ \"token\": \"fdca03ce3cf55a3bc5612f91f69176e5632826118694a53ecf4e1622cfa5c7013b52a195faafe0bfe91bec406ed99735\", \"request\": { \"interface\": \"ServiceInterface\", \"method\": \"getAllUsers\", \"parameters\": { } } }"; MediaType JSON = MediaType.parse("application/json; charset=utf-8"); RequestBody body = RequestBody.create(JSON, s); return ApiConnection.createPOST(API_BASE_URL, body).requestSyncCall(); } private String getUserDetailsFromApi(int userId) throws MalformedURLException { String apiUrl = API_URL_GET_USER_DETAILS + userId + ".json"; return ApiConnection.createGET(apiUrl).requestSyncCall(); } /** * Checks if the device has any active internet connection. * * @return true device with internet connection, otherwise false. */ private boolean isThereInternetConnection() { boolean isConnected; ConnectivityManager connectivityManager = (ConnectivityManager) this.context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); isConnected = (networkInfo != null && networkInfo.isConnectedOrConnecting()); return isConnected; } }