use of com.squareup.okhttp.Response in project cw-omnibus by commonsguy.
the class BackupService method uploadBackup.
private void uploadBackup(File backup) throws IOException {
Request request = new Request.Builder().url(URL_CREATE_BACKUP).post(RequestBody.create(JSON, "{}")).build();
Response response = OKHTTP_CLIENT.newCall(request).execute();
if (response.code() == 201) {
String backupURL = response.header("Location");
request = new Request.Builder().url(backupURL + RESOURCE_DATASET).put(RequestBody.create(ZIP, backup)).build();
response = OKHTTP_CLIENT.newCall(request).execute();
if (response.code() == 201) {
String datasetURL = response.header("Location");
SharedPreferences prefs = getSharedPreferences(getClass().getName(), Context.MODE_PRIVATE);
prefs.edit().putString(PREF_LAST_BACKUP_DATASET, datasetURL).commit();
} else {
Log.e(getClass().getSimpleName(), "Unsuccessful request to upload backup");
}
} else {
Log.e(getClass().getSimpleName(), "Unsuccessful request to create backup");
}
}
use of com.squareup.okhttp.Response in project fresco by facebook.
the class OkHttpNetworkFetcher method fetchWithRequest.
protected void fetchWithRequest(final OkHttpNetworkFetchState fetchState, final Callback callback, final Request request) {
final Call call = mOkHttpClient.newCall(request);
fetchState.getContext().addCallbacks(new BaseProducerContextCallbacks() {
@Override
public void onCancellationRequested() {
if (Looper.myLooper() != Looper.getMainLooper()) {
call.cancel();
} else {
mCancellationExecutor.execute(new Runnable() {
@Override
public void run() {
call.cancel();
}
});
}
}
});
call.enqueue(new com.squareup.okhttp.Callback() {
@Override
public void onResponse(Response response) {
fetchState.responseTime = SystemClock.uptimeMillis();
final ResponseBody body = response.body();
try {
if (!response.isSuccessful()) {
handleException(call, new IOException("Unexpected HTTP code " + response), callback);
return;
}
long contentLength = body.contentLength();
if (contentLength < 0) {
contentLength = 0;
}
callback.onResponse(body.byteStream(), (int) contentLength);
} catch (Exception e) {
handleException(call, e, callback);
} finally {
try {
body.close();
} catch (Exception e) {
FLog.w(TAG, "Exception when closing response body", e);
}
}
}
@Override
public void onFailure(final Request request, final IOException e) {
handleException(call, e, callback);
}
});
}
use of com.squareup.okhttp.Response in project Notes by lguipeng.
the class TAndroidTransport method flush.
@Override
public void flush() throws TTransportException {
Util.closeQuietly(mResponseBody);
mResponseBody = null;
RequestBody requestBody = new RequestBody() {
@Override
public MediaType contentType() {
if (mHeaders != null && mHeaders.containsKey("Content-Type")) {
return MediaType.parse(mHeaders.get("Content-Type"));
} else {
return MEDIA_TYPE_THRIFT;
}
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
copy(mByteStore.getInputStream(), sink.outputStream());
}
};
try {
Request.Builder builder = new Request.Builder().url(mUrl).post(requestBody);
if (mHeaders != null) {
for (String name : mHeaders.keySet()) {
builder.header(name, mHeaders.get(name));
}
}
Response response = mHttpClient.newCall(builder.build()).execute();
if (response.code() != 200) {
throw new TTransportException("HTTP Response code: " + response.code() + ", message " + response.message());
}
mResponseBody = response.body().byteStream();
} catch (Exception e) {
throw new TTransportException(e);
} finally {
try {
mByteStore.reset();
} catch (IOException ignored) {
}
}
}
use of com.squareup.okhttp.Response in project actor-platform by actorapp.
the class ActorPushRegister method registerForPush.
public static void registerForPush(final Context context, String endpoint, final Callback callback) {
Runtime.dispatch(() -> {
final SharedPreferences sharedPreferences = context.getSharedPreferences("actor_push_register", Context.MODE_PRIVATE);
String registrationEndpoint = sharedPreferences.getString("registration_endpoint", null);
String registrationData = sharedPreferences.getString("registration_data", null);
OkHttpClient client = new OkHttpClient();
if (registrationEndpoint != null && registrationData != null) {
try {
JSONObject data = new JSONObject(registrationData);
startService(data, context);
callback.onRegistered(registrationEndpoint);
return;
} catch (JSONException e) {
e.printStackTrace();
sharedPreferences.edit().clear().commit();
}
}
final Request request = new Request.Builder().url(endpoint).method("POST", RequestBody.create(MediaType.parse("application/json"), "{}")).build();
client.newCall(request).enqueue(new com.squareup.okhttp.Callback() {
@Override
public void onFailure(Request request, IOException e) {
Log.d("ACTOR_PUSH", "ACTOR_PUSH not registered: " + e.getMessage());
}
@Override
public void onResponse(Response response) throws IOException {
try {
String res = response.body().string();
JSONObject js = new JSONObject(res).getJSONObject("data");
String endpoint1 = js.getString("endpoint");
sharedPreferences.edit().putString("registration_endpoint", endpoint1).putString("registration_data", js.toString()).commit();
startService(js, context);
Log.d("ActorPushRegister", "Endpoint: " + endpoint1);
callback.onRegistered(endpoint1);
} catch (JSONException e) {
e.printStackTrace();
// TODO: Handle?
}
}
});
});
}
use of com.squareup.okhttp.Response in project remusic by aa112901.
the class HttpUtil method postUrl.
public static void postUrl(Context context, String j) {
try {
String action = "https://music.163.com/weapi/login/";
RequestBody formBody = new FormEncodingBuilder().build();
Log.e("post", "p");
Request request = new Request.Builder().url(action).header("Content-Type", "application/x-www-form-urlencoded").header("Host", "music.163.com").header("Cookie", "appver=1.5.0.75771").header("Referer", "http://music.163.com/").header("Connection", "keep-alive").header("Accept-Encoding", "gzip,deflate").header("Accept", "*/*").header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36").post(formBody).build();
mOkHttpClient.setCookieHandler(new CookieManager(new PersistentCookieStore(context.getApplicationContext()), CookiePolicy.ACCEPT_ALL));
Response response = mOkHttpClient.newCall(request).execute();
if (response.isSuccessful()) {
Log.e("respose", response.body().string());
}
} catch (Exception e) {
e.printStackTrace();
}
}
Aggregations