use of com.squareup.okhttp.Callback in project PocketHub by pockethub.
the class EditIssueActivity method onActivityResult.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_SELECT_PHOTO && resultCode == Activity.RESULT_OK) {
progressDialog = new MaterialDialog.Builder(this).content(R.string.loading).progress(true, 0).build();
progressDialog.show();
ImageBinPoster.post(this, data.getData(), new Callback() {
@Override
public void onFailure(Request request, IOException e) {
progressDialog.dismiss();
showImageError();
}
@Override
public void onResponse(com.squareup.okhttp.Response response) throws IOException {
progressDialog.dismiss();
if (response.isSuccessful()) {
insertImage(ImageBinPoster.getUrl(response.body().string()));
} else {
showImageError();
}
}
});
}
}
use of com.squareup.okhttp.Callback 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.Callback 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()));
}
}
});
});
}
use of com.squareup.okhttp.Callback 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;
}
use of com.squareup.okhttp.Callback 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);
}
}
});
}
Aggregations