Skip to content

Instantly share code, notes, and snippets.

@aldoKelvianto
Created February 23, 2016 17:10
Show Gist options
  • Save aldoKelvianto/62d27df05eddb7d5f642 to your computer and use it in GitHub Desktop.
Save aldoKelvianto/62d27df05eddb7d5f642 to your computer and use it in GitHub Desktop.
private void experimentPostQuestion(){
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
String userName = "admin";
String password = "pass";
String base64EncodedCredentials = "Basic " + Base64.encodeToString(
(userName + ":" + password).getBytes(),
Base64.NO_WRAP);
okhttp3.Request original = chain.request();
okhttp3.Request.Builder requestBuilder = original.newBuilder()
.header("Authorization", base64EncodedCredentials)
.header("Accept", "application/json")
.method(original.method(), original.body());
okhttp3.Request request = requestBuilder.build();
return chain.proceed(request);
}
});
OkHttpClient client = httpClient.build();
final String BASE_URL = "http://yourapi.com/api/";
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
APIService apiService = retrofit.create(APIService.class);
LocationBody locationBody = new LocationBody(-6.92f, 107.61f);
Call<LocationID> locationIDCall = apiService.loadLocationWithoutHeader(locationBody);
locationIDCall.enqueue(new Callback<LocationID>() {
@Override
public void onResponse(Call<LocationID> call, retrofit2.Response<LocationID> response) {
Log.d("asdf", "loc id" + response.body().getResult().getLOCID());
Log.d("asdf", "loc name " + response.body().getResult().getLOCNAME());
}
@Override
public void onFailure(Call<LocationID> call, Throwable t) {
Log.d("asdf", "failure " + t.getMessage());
}
});
}
private void callPostQuestion(){
final String BASE_URL = "http://yourapi.com/api/";
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
APIService apiService = retrofit.create(APIService.class);
LocationBody locationBody = new LocationBody(-6.92f, 107.61f);
Call<LocationID> locationIDCall = apiService.loadLocation(locationBody);
locationIDCall.enqueue(new Callback<LocationID>() {
@Override
public void onResponse(Call<LocationID> call, retrofit2.Response<LocationID> response) {
Log.d("asdf", "loc id" + response.body().getResult().getLOCID());
Log.d("asdf", "loc name " + response.body().getResult().getLOCNAME());
}
@Override
public void onFailure(Call<LocationID> call, Throwable t) {
Log.d("asdf", "failure " + t.getMessage());
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment