use of zipkin2.Callback in project BaseProject by wareine.
the class UploadFileManager method upLoadFile.
/**
* 上传文件
* @param actionUrl 接口地址
* @param filePath 本地文件地址
*/
public <T> void upLoadFile(String actionUrl, String filePath, final ReqCallBack<T> callBack) {
// 补全请求地址
String requestUrl = String.format("%s/%s", BASE_URL, actionUrl);
// 创建File
File file = new File(filePath);
// 创建RequestBody
RequestBody body = RequestBody.create(MEDIA_OBJECT_STREAM, file);
// 创建Request
final Request request = new Request.Builder().url(requestUrl).post(body).build();
final Call call = new OkHttpClient.Builder().writeTimeout(50, TimeUnit.SECONDS).build().newCall(request);
call.enqueue(new okhttp3.Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e(TAG, e.toString());
failedCallBack("上传失败", callBack);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
String string = response.body().string();
Log.e(TAG, "response ----->" + string);
successCallBack((T) string, callBack);
} else {
failedCallBack("上传失败", callBack);
}
}
});
}
use of zipkin2.Callback in project BaseProject by wareine.
the class UploadFileManager method upLoadFile.
/**
*上传文件
* @param actionUrl 接口地址
* @param paramsMap 参数
* @param callBack 回调
* @param <T>
*/
public <T> void upLoadFile(String actionUrl, HashMap<String, Object> paramsMap, final ReqProgressCallBack<T> callBack) {
try {
// 补全请求地址
String requestUrl = String.format("%s/%s", BASE_URL, actionUrl);
MultipartBody.Builder builder = new MultipartBody.Builder();
// 设置类型
builder.setType(MultipartBody.FORM);
// 追加参数
for (String key : paramsMap.keySet()) {
Object object = paramsMap.get(key);
if (!(object instanceof File)) {
builder.addFormDataPart(key, object.toString());
} else {
File file = (File) object;
builder.addFormDataPart(key, file.getName(), createProgressRequestBody(MEDIA_OBJECT_STREAM, file, callBack));
}
}
// 创建RequestBody
RequestBody body = builder.build();
// 创建Request
final Request request = new Request.Builder().url(requestUrl).post(body).build();
final Call call = mOkHttpClient.newBuilder().writeTimeout(50, TimeUnit.SECONDS).build().newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e(TAG, e.toString());
failedCallBack("上传失败", callBack);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
String string = response.body().string();
Log.e(TAG, "response ----->" + string);
successCallBack((T) string, callBack);
} else {
failedCallBack("上传失败", callBack);
}
}
});
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
use of zipkin2.Callback in project BaseProject by wareine.
the class UploadFileManager method downLoadFile.
/**
* 下载文件
* @param fileUrl 文件url
* @param destFileDir 存储目标目录
*/
public <T> void downLoadFile(String fileUrl, final String destFileDir, final ReqProgressCallBack<T> callBack) {
final String fileName = EncryptUtils.encryptMD5ToString(fileUrl);
final File file = new File(destFileDir, fileName);
if (file.exists()) {
successCallBack((T) file, callBack);
return;
}
final Request request = new Request.Builder().url(fileUrl).build();
final Call call = mOkHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e(TAG, e.toString());
failedCallBack("下载失败", callBack);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
InputStream is = null;
byte[] buf = new byte[2048];
int len = 0;
FileOutputStream fos = null;
try {
long total = response.body().contentLength();
Log.e(TAG, "total------>" + total);
long current = 0;
is = response.body().byteStream();
fos = new FileOutputStream(file);
while ((len = is.read(buf)) != -1) {
current += len;
fos.write(buf, 0, len);
Log.e(TAG, "current------>" + current);
progressCallBack(total, current, callBack);
}
fos.flush();
successCallBack((T) file, callBack);
} catch (IOException e) {
Log.e(TAG, e.toString());
failedCallBack("下载失败", callBack);
} finally {
try {
if (is != null) {
is.close();
}
if (fos != null) {
fos.close();
}
} catch (IOException e) {
Log.e(TAG, e.toString());
}
}
}
});
}
use of zipkin2.Callback in project Tusky by tuskyapp.
the class AccountActivity method mute.
private void mute(final String id) {
Callback<Relationship> cb = new Callback<Relationship>() {
@Override
public void onResponse(@NonNull Call<Relationship> call, @NonNull Response<Relationship> response) {
Relationship relationship = response.body();
if (response.isSuccessful() && relationship != null) {
broadcast(TimelineReceiver.Types.MUTE_ACCOUNT, id);
muting = relationship.getMuting();
updateButtons();
} else {
onMuteFailure(id);
}
}
@Override
public void onFailure(@NonNull Call<Relationship> call, @NonNull Throwable t) {
onMuteFailure(id);
}
};
if (muting) {
mastodonApi.unmuteAccount(id).enqueue(cb);
} else {
mastodonApi.muteAccount(id).enqueue(cb);
}
}
use of zipkin2.Callback in project Tusky by tuskyapp.
the class AccountActivity method follow.
private void follow(final String id) {
Callback<Relationship> cb = new Callback<Relationship>() {
@Override
public void onResponse(@NonNull Call<Relationship> call, @NonNull Response<Relationship> response) {
Relationship relationship = response.body();
if (response.isSuccessful() && relationship != null) {
if (relationship.getFollowing()) {
followState = FollowState.FOLLOWING;
} else if (relationship.getRequested()) {
followState = FollowState.REQUESTED;
Snackbar.make(container, R.string.state_follow_requested, Snackbar.LENGTH_LONG).show();
} else {
followState = FollowState.NOT_FOLLOWING;
broadcast(TimelineReceiver.Types.UNFOLLOW_ACCOUNT, id);
}
updateButtons();
} else {
onFollowFailure(id);
}
}
@Override
public void onFailure(@NonNull Call<Relationship> call, @NonNull Throwable t) {
onFollowFailure(id);
}
};
Assert.expect(followState != FollowState.REQUESTED);
switch(followState) {
case NOT_FOLLOWING:
{
mastodonApi.followAccount(id).enqueue(cb);
break;
}
case FOLLOWING:
{
mastodonApi.unfollowAccount(id).enqueue(cb);
break;
}
}
}
Aggregations