[英]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.