use of com.squareup.okhttp.RequestBody in project xDrip by NightscoutFoundation.
the class GzipRequestInterceptor method sendFeedback.
public void sendFeedback(View myview) {
final EditText contact = (EditText) findViewById(R.id.contactText);
final EditText yourtext = (EditText) findViewById(R.id.yourText);
final OkHttpClient client = new OkHttpClient();
client.setConnectTimeout(10, TimeUnit.SECONDS);
client.setReadTimeout(30, TimeUnit.SECONDS);
client.setWriteTimeout(30, TimeUnit.SECONDS);
client.interceptors().add(new GzipRequestInterceptor());
if (yourtext.length() == 0) {
toast("No text entered - cannot send blank");
return;
}
if (contact.length() == 0) {
toast("Without some contact info we cannot reply");
askEmailAddress();
return;
}
if (type_of_message.equals("Unknown")) {
askType();
return;
}
PersistentStore.setString(FEEDBACK_CONTACT_REFERENCE, contact.getText().toString());
toast("Sending..");
try {
final RequestBody formBody = new FormEncodingBuilder().add("contact", contact.getText().toString()).add("body", JoH.getDeviceDetails() + "\n" + JoH.getVersionDetails() + "\n" + getBestCollectorHardwareName() + "\n===\n\n" + yourtext.getText().toString() + " \n\n===\nType: " + type_of_message + "\nLog data:\n\n" + log_data + "\n\n\nSent: " + JoH.dateTimeText(JoH.tsl())).add("rating", String.valueOf(myrating.getRating())).add("type", type_of_message).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 feedback request");
final Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
JoH.static_toast_long(response.body().string());
log_data = "";
// Home.toaststatic("Feedback sent successfully");
finish();
} else {
JoH.static_toast_short("Error sending feedback: " + response.message().toString());
}
} catch (Exception e) {
Log.e(TAG, "Got exception in execute: " + e.toString());
JoH.static_toast_short("Error with network connection");
}
}
}).start();
} catch (Exception e) {
JoH.static_toast_short(e.getMessage());
Log.e(TAG, "General exception: " + e.toString());
}
}
use of com.squareup.okhttp.RequestBody in project xDrip by NightscoutFoundation.
the class GzipRequestInterceptor method gzip.
private RequestBody gzip(final RequestBody body) {
return new RequestBody() {
@Override
public MediaType contentType() {
return body.contentType();
}
@Override
public long contentLength() {
// We don't know the compressed length in advance!
return -1;
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
BufferedSink gzipSink = Okio.buffer(new GzipSink(sink));
body.writeTo(gzipSink);
gzipSink.close();
}
};
}
use of com.squareup.okhttp.RequestBody in project xDrip-plus by jamorham.
the class GzipRequestInterceptor method gzip.
private RequestBody gzip(final RequestBody body) {
return new RequestBody() {
@Override
public MediaType contentType() {
return body.contentType();
}
@Override
public long contentLength() {
// We don't know the compressed length in advance!
return -1;
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
BufferedSink gzipSink = Okio.buffer(new GzipSink(sink));
body.writeTo(gzipSink);
gzipSink.close();
}
};
}
use of com.squareup.okhttp.RequestBody 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());
}
}
Aggregations