use of okhttp3.ResponseBody in project okhttp by square.
the class OkHttpContributors method main.
public static void main(String... args) throws Exception {
OkHttpClient client = new OkHttpClient();
// Create request for remote resource.
Request request = new Request.Builder().url(ENDPOINT).build();
// Execute the request and retrieve the response.
Response response = client.newCall(request).execute();
// Deserialize HTTP response to concrete type.
ResponseBody body = response.body();
List<Contributor> contributors = CONTRIBUTORS_JSON_ADAPTER.fromJson(body.source());
body.close();
// Sort list by the most contributions.
Collections.sort(contributors, new Comparator<Contributor>() {
@Override
public int compare(Contributor c1, Contributor c2) {
return c2.contributions - c1.contributions;
}
});
// Output list of contributors.
for (Contributor contributor : contributors) {
System.out.println(contributor.login + ": " + contributor.contributions);
}
}
use of okhttp3.ResponseBody in project picasso by square.
the class NetworkRequestHandlerTest method unknownContentLengthFromDiskThrows.
@Test
public void unknownContentLengthFromDiskThrows() throws Exception {
final AtomicBoolean closed = new AtomicBoolean();
ResponseBody body = new ResponseBody() {
@Override
public MediaType contentType() {
return null;
}
@Override
public long contentLength() {
return 0;
}
@Override
public BufferedSource source() {
return new Buffer();
}
@Override
public void close() {
closed.set(true);
super.close();
}
};
responses.add(responseOf(body).newBuilder().cacheResponse(responseOf(null)).build());
Action action = TestUtils.mockAction(URI_KEY_1, URI_1);
try {
networkHandler.load(action.getRequest(), 0);
fail();
} catch (IOException expected) {
verifyZeroInteractions(stats);
assertTrue(closed.get());
}
}
use of okhttp3.ResponseBody in project glide by bumptech.
the class OkHttpStreamFetcher method loadData.
@Override
public void loadData(Priority priority, final DataCallback<? super InputStream> callback) {
Request.Builder requestBuilder = new Request.Builder().url(url.toStringUrl());
for (Map.Entry<String, String> headerEntry : url.getHeaders().entrySet()) {
String key = headerEntry.getKey();
requestBuilder.addHeader(key, headerEntry.getValue());
}
Request request = requestBuilder.build();
call = client.newCall(request);
call.enqueue(new okhttp3.Callback() {
@Override
public void onFailure(Call call, IOException e) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "OkHttp failed to obtain result", e);
}
callback.onLoadFailed(e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
responseBody = response.body();
if (response.isSuccessful()) {
long contentLength = responseBody.contentLength();
stream = ContentLengthInputStream.obtain(responseBody.byteStream(), contentLength);
} else if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "OkHttp got error response: " + response.code() + ", " + response.message());
}
callback.onDataReady(stream);
}
});
}
use of okhttp3.ResponseBody in project amhttp by Eddieyuan123.
the class OnAddListener method parseNetworkResponse.
@Override
public void parseNetworkResponse(Response response, FileCard fileCard) throws Throwable {
ResponseBody responseBody = response.body();
if (responseBody == null) {
throw new NullPointerException("response body is null");
} else {
String responseStr = responseBody.string();
Type type = ClassUtils.getType(OnAddListener.this.getClass());
T bean = null;
if (type != null) {
if (TextUtils.equals(type.toString(), "class java.lang.String"))
bean = (T) responseStr;
else
bean = new Gson().fromJson(responseStr, type);
}
final T finalBean = bean;
Dispatcher dispatcher = Dispatcher.getDispatcher(Looper.getMainLooper());
dispatcher.dispatchToUIThread(new Runnable() {
@Override
public void run() {
onResponseSuccess(finalBean);
}
});
}
}
use of okhttp3.ResponseBody in project amhttp by Eddieyuan123.
the class OnUploadListener method parseNetworkResponse.
@Override
public void parseNetworkResponse(Response response, FileCard fileCard) throws Throwable {
ResponseBody responseBody = response.body();
if (responseBody == null) {
throw new NullPointerException("response body is null");
} else {
String responseStr = responseBody.string();
Type type = ClassUtils.getType(OnUploadListener.this.getClass());
T bean = null;
if (type != null) {
if (TextUtils.equals(type.toString(), "class java.lang.String"))
bean = (T) responseStr;
else
bean = new Gson().fromJson(responseStr, type);
}
final T finalBean = bean;
Dispatcher dispatcher = Dispatcher.getDispatcher(Looper.getMainLooper());
dispatcher.dispatchToUIThread(new Runnable() {
@Override
public void run() {
onResponseSuccess(finalBean);
}
});
}
}
Aggregations