use of com.squareup.okhttp.Response in project xDrip-plus by jamorham.
the class WebAppHelper method doInBackground.
@Override
protected Integer doInBackground(String... url) {
try {
Log.d(TAG, "Processing URL: " + url[0]);
Request request = new Request.Builder().header("User-Agent", "Mozilla/5.0 (jamorham)").header("Connection", "close").url(url[0]).build();
client.setConnectTimeout(15, TimeUnit.SECONDS);
client.setReadTimeout(30, TimeUnit.SECONDS);
client.setWriteTimeout(30, TimeUnit.SECONDS);
final Response response = client.newCall(request).execute();
if (!response.isSuccessful())
throw new IOException("Unexpected code " + response);
body = response.body().bytes();
} catch (Exception e) {
Log.d(TAG, "Exception in background task: " + e.toString());
}
return body.length;
}
use of com.squareup.okhttp.Response in project xDrip-plus by jamorham.
the class LanguageEditor method uploadData.
private void uploadData(String data) {
if (data.length() < 10) {
// don't translate
toast("No text entered - cannot send blank");
return;
}
final String email = LanguageStore.getString(EMAIL_KEY);
final String name = LanguageStore.getString(NAME_KEY);
final String consent = LanguageStore.getString(CONSENT_KEY);
if (email.length() == 0) {
// don't translate
toast("No email address stored - cannot send");
return;
}
if (name.length() == 0) {
// don't translate
toast("No thanks name stored - cannot send");
return;
}
if (consent.length() == 0) {
// don't translate
toast("No consent stored - cannot send");
return;
}
final OkHttpClient client = new OkHttpClient();
final String send_url = mContext.getString(R.string.wserviceurl) + "/joh-langdata";
client.setConnectTimeout(20, TimeUnit.SECONDS);
client.setReadTimeout(30, TimeUnit.SECONDS);
client.setWriteTimeout(30, TimeUnit.SECONDS);
// don't translate
toast("Sending..");
try {
final RequestBody formBody = new FormEncodingBuilder().add("locale", Locale.getDefault().toString()).add("contact", email).add("name", name).add("consent", consent).add("data", data).build();
new Thread(new Runnable() {
public void run() {
try {
final Request request = new Request.Builder().url(send_url).post(formBody).build();
Log.i(TAG, "Sending language data");
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
toast("data sent successfully");
((Activity) mContext).runOnUiThread(new Runnable() {
@Override
public void run() {
try {
saveBtn.setVisibility(View.INVISIBLE);
undoBtn.setVisibility(View.INVISIBLE);
} catch (Exception e) {
// do nothing if its gone away
}
}
});
} else {
toast("Error sending data: " + response.message());
}
} catch (Exception e) {
Log.e(TAG, "Got exception in execute: " + e.toString());
// don't translate
toast("Error with network connection");
}
}
}).start();
} catch (Exception e) {
toast(e.getMessage());
Log.e(TAG, "General exception: " + e.toString());
}
}
use of com.squareup.okhttp.Response in project SimplifyReader by chentao0707.
the class OkHttpStack method performRequest.
@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError {
OkHttpClient client = mClient.clone();
int timeoutMs = request.getTimeoutMs();
client.setConnectTimeout(timeoutMs, TimeUnit.MILLISECONDS);
client.setReadTimeout(timeoutMs, TimeUnit.MILLISECONDS);
client.setWriteTimeout(timeoutMs, TimeUnit.MILLISECONDS);
com.squareup.okhttp.Request.Builder okHttpRequestBuilder = new com.squareup.okhttp.Request.Builder();
okHttpRequestBuilder.url(request.getUrl());
Map<String, String> headers = request.getHeaders();
for (final String name : headers.keySet()) {
okHttpRequestBuilder.addHeader(name, headers.get(name));
}
for (final String name : additionalHeaders.keySet()) {
okHttpRequestBuilder.addHeader(name, additionalHeaders.get(name));
}
setConnectionParametersForRequest(okHttpRequestBuilder, request);
com.squareup.okhttp.Request okHttpRequest = okHttpRequestBuilder.build();
Call okHttpCall = client.newCall(okHttpRequest);
Response okHttpResponse = okHttpCall.execute();
StatusLine responseStatus = new BasicStatusLine(parseProtocol(okHttpResponse.protocol()), okHttpResponse.code(), okHttpResponse.message());
BasicHttpResponse response = new BasicHttpResponse(responseStatus);
response.setEntity(entityFromOkHttpResponse(okHttpResponse));
Headers responseHeaders = okHttpResponse.headers();
for (int i = 0, len = responseHeaders.size(); i < len; i++) {
final String name = responseHeaders.name(i), value = responseHeaders.value(i);
if (name != null) {
response.addHeader(new BasicHeader(name, value));
}
}
return response;
}
use of com.squareup.okhttp.Response 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.Response in project pictureapp by EyeSeeTea.
the class ServerAPIController method getServerVersion.
/**
* Returns the version of the given server.
* Null if something went wrong
*/
public static String getServerVersion(String url) throws ApiCallException {
String serverVersion = null;
String urlServerInfo = url + DHIS_SERVER_INFO;
Response response = ServerApiCallExecution.executeCall(null, urlServerInfo, "GET");
JSONObject body = ServerApiUtils.getApiResponseAsJSONObject(response);
if (body.has(TAG_VERSION)) {
try {
serverVersion = body.getString(TAG_VERSION);
} catch (JSONException e) {
new ApiCallException(e);
}
}
if (serverVersion != null) {
Log.i(TAG, String.format("getServerVersion(%s) -> %s", url, serverVersion));
}
return serverVersion;
}
Aggregations