compile 'com.squareup.retrofit:retrofit:1.9.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'
compile 'com.squareup.okhttp:okhttp:2.0.0'
HttpUrlConnection-way
HttpURLConnection getTodoConnection = null;
try {
getTodoConnection = (HttpURLConnection) mGetQueryUrl.openConnection();
} catch (IOException e) {
Log.e(TAG, "Error: " + e.getMessage());
}
try {
String response = parseResponse(getTodoConnection.getInputStream());
} catch (IOException e) {
Log.e(TAG, "Error: " + e.getMessage());
} finally {
getTodoConnection.disconnect();
}
// Send response somewhere (eg. Event bus or LocalBroadcastReceiver)
Retrofit-way
public interface TodoApi { @GET("/todos")
void getTodosByUserId(@Query("userId") Long userId, Callback<Todo[]> todos);
}
... then create a RestAdapter once and create,
RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(Api.BASE_URL).build();
mTodoApi = restAdapter.create(TodoApi.class);
mTodoApi.getTodosByUserId(1L, new Callback<Todo[]>() {
@Override
public void success(Todo[] todos, Response response) {
// Do something on success
}
@Override
public void failure(RetrofitError error) {
// Do something on failure
}
});
public interface TodoApi {
@FormUrlEncoded
@POST("/todos")
void addTodo(@Field("userId") Long userId, @Field("id") Long id,
@Field("title") String title, @Field("completed") boolean completed,
Callback<Todo> todo);
@POST("/todos")
void addTodo(@Body Todo newTodo, Callback<Todo> todo);
}
mTodoApi.addTodo(newTodo, new Callback<Todo>() {
@Override
public void success(Todo todo, Response response) {
// Do something on success
}
@Override
public void failure(RetrofitError error) {
// Do something on failure
}
});
public class ThrottlingInterceptor implements RequestInterceptor {
private static final String TAG = ThrottlingInterceptor.class.getSimpleName();
private long lastRequest = 0L;
private final long maxRequestSpeed;
private final Lock requestLock = new ReentrantLock();
public ThrottlingInterceptor(long maxRequestSpeed) {
this.maxRequestSpeed = maxRequestSpeed;
}
@Override
public void intercept(RequestFacade request) {
requestLock.lock();
try {
long curTime = System.currentTimeMillis();
long diff = curTime - lastRequest;
if (diff < maxRequestSpeed)
try {
Thread.sleep(maxRequestSpeed - diff);
} catch (InterruptedException e) {
Log.e(TAG, "Error: " + e.getMessage());
}
lastRequest = System.currentTimeMillis();
} finally { requestLock.unlock(); }
}
}
RestAdapter restAdapter = new RestAdapter.Builder()
// Throttle every 1s
.setRequestInterceptor(new ThrottlingInterceptor(1000))
.setEndpoint(Api.BASE_URL).build();
mTodoApi = restAdapter.create(TodoApi.class);
public class TodoErrorHandler implements ErrorHandler {
// Variable declarations and constructor...
@Override
public Throwable handleError(RetrofitError cause) {
if (cause.getKind() == RetrofitError.Kind.NETWORK) {
mToastHandler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(mContext, R.string.error_network, Toast.LENGTH_SHORT).show();
}
});
} else if (cause.getKind() == RetrofitError.Kind.HTTP) {
mToastHandler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(mContext, R.string.error_http, Toast.LENGTH_SHORT).show();
}
});
}
return cause.getCause();
}
}