简体   繁体   English

Android:如何替换已弃用的 HttpRequestExecutor

[英]Android: How to replace deprecated HttpRequestExecutor

I am trying to remove the HttpClient api from my Android project and to transition to using HttpURLConnection.我正在尝试从我的 Android 项目中删除 HttpClient api 并转换为使用 HttpURLConnection。

In the old API, I made use of HttpRequestExecutor, to change some icon in the notification bar when the app is downloading vs uploading在旧的 API 中,我使用了 HttpRequestExecutor,在应用程序下载和上传时更改通知栏中的一些图标

this.httpclient = new DefaultHttpClient(httpParameters){
        @Override
        protected HttpRequestExecutor createRequestExecutor() {
            return new HttpRequestExecutor(){

                @Override
                protected HttpResponse doSendRequest(HttpRequest request,
                        HttpClientConnection conn, HttpContext http_context)
                                throws IOException, HttpException {

                    EventsBroadcaster.broadcastConnectionUploading(context);
                    return super.doSendRequest(request, conn, http_context);
                }

                @Override
                protected HttpResponse doReceiveResponse(
                        HttpRequest request, HttpClientConnection conn,
                        HttpContext http_context) throws HttpException,
                        IOException {

                    EventsBroadcaster.broadcastConnectionDownloading(context);
                    return super.doReceiveResponse(request, conn, http_context);
                }
            };
        }
    };

How can I do the same with HttpURLConnection?我怎样才能对 HttpURLConnection 做同样的事情?

`OkHttpClient client = new OkHttpClient(); `OkHttpClient 客户端 = new OkHttpClient();

    MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
    RequestBody body = RequestBody.create(mediaType, "Your Body");
    Request request = new Request.Builder()
            .url("Your url")
            .post(body)
            .addHeader("add as many add headers as u want")
            .build();
    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(@NotNull Call call, @NotNull IOException e) {
            //What should happen if failed
        }

        @Override
        public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
            //what should happen if it is successful
        }
    }); `

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为 android 4.0.3 替换 type_orientation(已弃用)? - How can i replace type_orientation (is deprecated) for android 4.0.3? 如何调试HttpRequestExecutor.execute(请求,连接,上下文);。? - How to Debug HttpRequestExecutor.execute(request, connection, context);.? 如何替换不推荐使用的构造函数DynamoDBMapperFieldModel - How to replace the deprecated constructor DynamoDBMapperFieldModel ReflectionUtils如何替换不推荐使用的方法? - ReflectionUtils how to replace deprecated methods? 如何替换不推荐使用的类方法? - How to replace deprecated class method? 如何在Hibernate中用TableGenerator替换已弃用的MultipleHiLoPerTableGenerator - How to replace deprecated MultipleHiLoPerTableGenerator with TableGenerator in Hibernate 如何替换已弃用的@MockClass? - How can I replace @MockClass, which was deprecated? 如何替换已弃用的csc ant任务 - How to replace the deprecated csc ant task 如何用 SecurityFilterChain 的多个过滤器替换已弃用的 WebSecurityConfigurerAdapter? - How to replace deprecated WebSecurityConfigurerAdapter with multiple filters by SecurityFilterChain? 如何替换 AnyLogic 中已弃用的 function RectangularNode? - How to replace deprecated function RectangularNode in AnyLogic?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM