use of com.squareup.okhttp.Request in project pictureapp by EyeSeeTea.
the class PushClient method pushData.
/**
* Pushes data to DHIS Server
*/
private JSONObject pushData(JSONObject data) throws ApiCallException {
Response response = null;
final String DHIS_URL = getDhisURL();
OkHttpClient client = UnsafeOkHttpsClientFactory.getUnsafeOkHttpClient();
// connect timeout
client.setConnectTimeout(30, TimeUnit.SECONDS);
// socket timeout
client.setReadTimeout(30, TimeUnit.SECONDS);
// write timeout
client.setWriteTimeout(30, TimeUnit.SECONDS);
// Cancel retry on failure
client.setRetryOnConnectionFailure(false);
BasicAuthenticator basicAuthenticator = new BasicAuthenticator();
client.setAuthenticator(basicAuthenticator);
RequestBody body = RequestBody.create(JSON, data.toString());
Request request = new Request.Builder().header(basicAuthenticator.AUTHORIZATION_HEADER, basicAuthenticator.getCredentials()).url(DHIS_URL).post(body).build();
try {
response = client.newCall(request).execute();
} catch (IOException e) {
throw new ApiCallException(e);
}
return ServerApiUtils.getApiResponseAsJSONObject(response);
}
use of com.squareup.okhttp.Request in project pictureapp by EyeSeeTea.
the class BasicAuthenticator method executeCall.
/**
* Call to DHIS Server
*/
static Response executeCall(JSONObject data, String url, String method) throws ApiCallException {
final String DHIS_URL = url;
Log.d(method, DHIS_URL);
OkHttpClient client = UnsafeOkHttpsClientFactory.getUnsafeOkHttpClient();
BasicAuthenticator basicAuthenticator = new BasicAuthenticator();
client.setAuthenticator(basicAuthenticator);
Request.Builder builder = new Request.Builder().header(basicAuthenticator.AUTHORIZATION_HEADER, basicAuthenticator.getCredentials()).url(DHIS_URL);
switch(method) {
case "POST":
RequestBody postBody = RequestBody.create(JSON, data.toString());
builder.post(postBody);
break;
case "PUT":
RequestBody putBody = RequestBody.create(JSON, data.toString());
builder.put(putBody);
break;
case "PATCH":
RequestBody patchBody = RequestBody.create(JSON, data.toString());
builder.patch(patchBody);
break;
case "GET":
builder.get();
break;
}
Request request = builder.build();
Response response = null;
try {
response = client.newCall(request).execute();
} catch (IOException ex) {
throw new ApiCallException(ex);
}
return response;
}
use of com.squareup.okhttp.Request in project nmid-headline by miao1007.
the class WebViewFragment method trySetupWebview.
private void trySetupWebview() {
// http://202.202.43.205:8086/api/android/newscontent?category=1&id=194
url = HeadlineService.END_POINT + "/api/android/newscontent?id=" + feed.getIdmember() + "&category=" + feed.getCategory();
WebSettings settings = mWebView.getSettings();
mWebView.setWebContentsDebuggingEnabled(true);
settings.setTextZoom(WebViewPref.getWebViewTextZoom(getActivity()));
switch(WebViewPref.isAutoLoadImages(getActivity())) {
case 0:
settings.setLoadsImagesAutomatically(false);
break;
case 1:
break;
case 2:
if (!NetworkUtils.isWifiAviliable(getActivity())) {
settings.setLoadsImagesAutomatically(false);
}
break;
}
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(url).build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
String htmlData;
if (ThemePref.isNightMode(getActivity())) {
// Webview will use asserts/style_night.css
htmlData = "<link rel=\"stylesheet\" type=\"text/css\" href=\"style_night.css\" /> <body class= \"gloable\"> " + response.body().string() + "</body>";
} else {
// Webview will use asserts/style.css
htmlData = "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" /> <body class= \"gloable\"> " + response.body().string() + "</body>";
}
Log.d(TAG, Thread.currentThread().getName());
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
mWebView.loadDataWithBaseURL("file:///android_asset/", htmlData, MIME_TYPE, ENCODING, null);
}
});
}
});
}
use of com.squareup.okhttp.Request in project glide by bumptech.
the class OkHttpStreamFetcher method loadData.
@Override
public void loadData(@NonNull Priority priority, @NonNull 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();
client.newCall(request).enqueue(new com.squareup.okhttp.Callback() {
@Override
public void onFailure(Request request, IOException e) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "OkHttp failed to obtain result", e);
}
callback.onLoadFailed(e);
}
@Override
public void onResponse(Response response) throws IOException {
responseBody = response.body();
if (response.isSuccessful()) {
long contentLength = responseBody.contentLength();
stream = ContentLengthInputStream.obtain(responseBody.byteStream(), contentLength);
callback.onDataReady(stream);
} else {
callback.onLoadFailed(new HttpException(response.message(), response.code()));
}
}
});
}
use of com.squareup.okhttp.Request in project actor-platform by actorapp.
the class AndroidHttpProvider method getMethod.
@Override
public Promise<HTTPResponse> getMethod(String url, int startOffset, int size, int totalSize) {
return new Promise<>(resolver -> {
final Request request = new Request.Builder().url(url).addHeader("Range", "bytes=" + startOffset + "-" + (startOffset + size)).build();
Log.d(TAG, "Downloading part: " + request.toString());
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
Log.d(TAG, "Downloading part error: " + request.toString());
e.printStackTrace();
// TODO: Better error?
resolver.error(new HTTPError(0));
}
@Override
public void onResponse(Response response) throws IOException {
Log.d(TAG, "Downloading part response: " + request.toString() + " -> " + response.toString());
if (response.code() >= 200 && response.code() < 300) {
resolver.result(new HTTPResponse(response.code(), response.body().bytes()));
} else {
resolver.error(new HTTPError(response.code()));
}
}
});
});
}
Aggregations