-
-
Save bioyeneye/835f220d7d329e221078dc7ccd1c1641 to your computer and use it in GitHub Desktop.
HyperTrack Android <> REST API network call. Steps: build.gradle > RetrofitServiceGenerator.java > RetrofitService.java > NearbyUser.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dependencies { | |
... | |
//Add retrofit library for network calls | |
compile 'com.squareup.retrofit2:retrofit:2.1.0' | |
compile 'com.squareup.retrofit2:converter-gson:2.1.0' | |
... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//For nearby API you can pass actionId or latlng. For more info on nearbyAPI https://docs.hypertrack.com/api/entities/user.html#list-nearby-users | |
public void getNearbyUsers(String actionId, Latlng latlng){ | |
//Create retrofit service object | |
RetrofitService getNearbyUserService = RetrofitServiceGenerator.createService(RetrofitService.class,context); | |
Call<UserListResponse> call = getNearbyUserService.getNearByUsers(actionID,latLng.latitude +","+latLng.longitude); | |
call.enqueue(new Callback<UserListResponse>() { | |
@Override | |
public void onResponse(Call<UserListResponse> call, Response<UserListResponse> response) { | |
if(response.isSuccessful()){ | |
if(response.body() != null){ | |
List<User> userList = response.body().getResults(); | |
Log.d(TAG, "onResponse: "+userList); | |
} | |
} | |
else { | |
Toast.makeText(context, "Error Occurred: "+response.code(), Toast.LENGTH_SHORT).show(); | |
} | |
} | |
@Override | |
public void onFailure(Call<UserListResponse> call, Throwable t) { | |
Toast.makeText(context, "Error Occurred: ", Toast.LENGTH_SHORT).show(); | |
Log.e(TAG, "onFailure: ", t); | |
} | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.hypertrack.sendeta.network.retrofit; | |
import com.hypertrack.lib.models.User; | |
import retrofit2.Call; | |
import retrofit2.http.Body; | |
import retrofit2.http.POST; | |
import retrofit2.http.Path; | |
/** | |
* Created by Aman Jain on 07/12/17. | |
*/ | |
public interface RetrofitService { | |
// Nearby API https://docs.hypertrack.com/api/entities/user.html#list-nearby-users | |
@GET("users/nearby/") | |
Call<UserListResponse> getNearByUsers(@Query("action_id") String action_id, @Query("location") String latLng); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.hypertrack.sendeta.network.retrofit; | |
import android.content.Context; | |
import com.hypertrack.lib.internal.common.util.Utils; | |
import java.io.IOException; | |
import java.util.TimeZone; | |
import okhttp3.Interceptor; | |
import okhttp3.OkHttpClient; | |
import okhttp3.Request; | |
import okhttp3.Response; | |
import retrofit2.Retrofit; | |
import retrofit2.converter.gson.GsonConverterFactory; | |
/** | |
* Created by Aman Jain on 07/12/17. | |
*/ | |
public class RetrofitServiceGenerator { | |
public static final String API_BASE_URL = "https://api.hypertrack.com/api/v1/"; | |
private static final String TAG = RetrofitServiceGenerator.class.getSimpleName(); | |
private static Context mContext; | |
private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); | |
private static Retrofit.Builder builder = | |
new Retrofit.Builder() | |
.baseUrl(API_BASE_URL) | |
.addConverterFactory(GsonConverterFactory.create()); | |
public static <S> S createService(Class<S> serviceClass, Context context) { | |
mContext = context; | |
httpClient.interceptors().add(new Interceptor() { | |
@Override | |
public Response intercept(Interceptor.Chain chain) throws IOException { | |
Request original = chain.request(); | |
// Request customization: add request headers | |
Request.Builder requestBuilder = original.newBuilder() | |
.header("Authorization", "Token " + BuildConfig.HYPERTRACK_PK) | |
.header("User-Agent", "hypertrack-live-android") | |
.header("timezone", TimeZone.getDefault().getID()) | |
.header("Device-id", Utils.getDeviceId(mContext)) | |
.header("app-id", mContext.getPackageName()) | |
.method(original.method(), original.body()); | |
Request request = requestBuilder.build(); | |
return chain.proceed(request); | |
} | |
}); | |
OkHttpClient client = httpClient.build(); | |
Retrofit retrofit = builder.client(client).build(); | |
return retrofit.create(serviceClass); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment