use of com.squareup.okhttp.Request in project SimpleNews by liuling07.
the class OkHttpUtils method postRequest.
private void postRequest(String url, final ResultCallback callback, List<Param> params) {
Request request = buildPostRequest(url, params);
deliveryResult(callback, request);
}
use of com.squareup.okhttp.Request in project SimpleNews by liuling07.
the class OkHttpUtils method deliveryResult.
private void deliveryResult(final ResultCallback callback, Request request) {
mOkHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, final IOException e) {
sendFailCallback(callback, e);
}
@Override
public void onResponse(Response response) throws IOException {
try {
String str = response.body().string();
if (callback.mType == String.class) {
sendSuccessCallBack(callback, str);
} else {
Object object = JsonUtils.deserialize(str, callback.mType);
sendSuccessCallBack(callback, object);
}
} catch (final Exception e) {
LogUtils.e(TAG, "convert json failure", e);
sendFailCallback(callback, e);
}
}
});
}
use of com.squareup.okhttp.Request in project storymaker by StoryMaker.
the class StorymakerDownloadManager method downloadWithManager.
private void downloadWithManager(Uri uri, String title, String desc, Uri uriFile) {
initDownloadManager();
Timber.d("QUEUEING DOWNLOAD: " + uri.toString() + " -> " + uriFile.toString());
initReceivers();
DownloadManager.Request request = new DownloadManager.Request(uri).setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE).setAllowedOverRoaming(false).setTitle(title).setDescription(desc).setVisibleInDownloadsUi(false).setDestinationUri(uriFile);
File partFile = new File(uriFile.toString().replace(".tmp", ".part"));
if (partFile.exists()) {
long partBytes = partFile.length();
Timber.d("PARTIAL FILE " + partFile.getPath() + " FOUND, SETTING RANGE HEADER: " + "Range" + " / " + "bytes=" + Long.toString(partBytes) + "-");
request.addRequestHeader("Range", "bytes=" + Long.toString(partBytes) + "-");
} else {
Timber.d("PARTIAL FILE " + partFile.getPath() + " NOT FOUND, STARTING AT BYTE 0");
}
lastDownload = dManager.enqueue(request);
// have to enqueue first to get manager id
String uriString = uriFile.toString();
StorymakerQueueManager.addToQueue(context, Long.valueOf(lastDownload), uriString.substring(uriString.lastIndexOf("/") + 1), queueDao);
}
use of com.squareup.okhttp.Request in project PocketHub by pockethub.
the class ImageBinPoster method post.
/**
* Post the image to ImageBin
*
* @param bytes Bytes of the image to post
* @param callback Request callback
*/
public static void post(byte[] bytes, Callback callback) {
RequestBody requestBody = new MultipartBuilder().type(MultipartBuilder.FORM).addFormDataPart("file", "test", RequestBody.create(MediaType.parse("image/*"), bytes)).build();
Request request = new Request.Builder().url("https://imagebin.ca/upload.php").post(requestBody).build();
OkHttpClient client = new OkHttpClient();
Call call = client.newCall(request);
call.enqueue(callback);
}
use of com.squareup.okhttp.Request in project ButterRemote-Android by se-bastiaan.
the class PopcornTimeRpcClient method request.
/**
* Send JSON RPC request to the instance
* @param rpc Request data
* @param callback Callback for the request
* @return ResponseFuture
*/
private Call request(final RpcRequest rpc, final Callback callback) {
RequestBody requestBody = RequestBody.create(MEDIA_TYPE_JSON, mGson.toJson(rpc));
Request request = new Request.Builder().url(mUrl).header("Authorization", Credentials.basic(mUsername, mPassword)).post(requestBody).build();
Call call = mClient.newCall(request);
call.enqueue(new com.squareup.okhttp.Callback() {
@Override
public void onFailure(Request request, IOException e) {
callback.onCompleted(e, null);
}
@Override
public void onResponse(Response response) throws IOException {
RpcResponse result = null;
Exception e = null;
try {
if (response != null && response.isSuccessful()) {
String responseStr = response.body().string();
//LogUtils.d("PopcornTimeRpcClient", "Response: " + responseStr);
result = mGson.fromJson(responseStr, RpcResponse.class);
LinkedTreeMap<String, Object> map = result.getMapResult();
if (map.containsKey("popcornVersion")) {
mVersion = (String) map.get("popcornVersion");
}
}
} catch (Exception ex) {
ex.printStackTrace();
e = ex;
mVersion = ZERO_VERSION;
if (rpc.id == RequestId.GET_SELECTION.ordinal()) {
mVersion = "0.3.4";
}
}
callback.onCompleted(e, result);
}
});
return call;
}
Aggregations