Search in sources :

Example 36 with Callback

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);
            }
        }
    });
}
Also used : Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) IOException(java.io.IOException) Response(okhttp3.Response) Callback(okhttp3.Callback) File(java.io.File) RequestBody(okhttp3.RequestBody)

Example 37 with 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());
    }
}
Also used : Call(okhttp3.Call) Request(okhttp3.Request) IOException(java.io.IOException) IOException(java.io.IOException) Response(okhttp3.Response) Callback(okhttp3.Callback) MultipartBody(okhttp3.MultipartBody) File(java.io.File) RequestBody(okhttp3.RequestBody)

Example 38 with Callback

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());
                }
            }
        }
    });
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) Callback(okhttp3.Callback) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) Request(okhttp3.Request) IOException(java.io.IOException) File(java.io.File)

Example 39 with Callback

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);
    }
}
Also used : Response(retrofit2.Response) Call(retrofit2.Call) Callback(retrofit2.Callback) Relationship(com.keylesspalace.tusky.entity.Relationship) NonNull(android.support.annotation.NonNull)

Example 40 with Callback

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;
            }
    }
}
Also used : Response(retrofit2.Response) Call(retrofit2.Call) Callback(retrofit2.Callback) Relationship(com.keylesspalace.tusky.entity.Relationship) NonNull(android.support.annotation.NonNull)

Aggregations

Callback (okhttp3.Callback)173 IOException (java.io.IOException)137 Call (okhttp3.Call)132 Response (okhttp3.Response)132 Request (okhttp3.Request)110 Callback (retrofit2.Callback)42 Call (retrofit2.Call)41 Test (org.junit.Test)39 Response (retrofit2.Response)39 RequestBody (okhttp3.RequestBody)37 OkHttpClient (okhttp3.OkHttpClient)34 File (java.io.File)27 Context (android.content.Context)24 JSONObject (org.json.JSONObject)20 FormBody (okhttp3.FormBody)19 ArrayList (java.util.ArrayList)18 View (android.view.View)16 Intent (android.content.Intent)14 TextView (android.widget.TextView)14 GsonBuilder (com.google.gson.GsonBuilder)14