I was following a tutorial and I got to a point where a lot of the code is deprecated.
ArrayList dataToSend = new ArrayList<>();
d
I found that I can replace NameValuePair with
Not really.
but I have no idea about the others
The entire HttpClient API that ships with Android itself is deprecated. The solution is to use a different HTTP client:
HttpUrlConnection
, from standard JavaWith respect to the tutorial, either:
You should try Retrofit, it's more simple to use that library instead of perform http requests. It was made for simplify communication between java and REST API.
square.github.io/retrofit/
I give you a sample from the documentation
public interface GitHubService {
@GET("/users/{user}/repos")
List<Repo> listRepos(@Path("user") String user);
}
The RestAdapter class generates an implementation of the GitHubService interface.
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("https://api.github.com")
.build();
GitHubService service = restAdapter.create(GitHubService.class);
Each call on the generated GitHubService makes an HTTP request to the remote webserver.
List<Repo> repos = service.listRepos("octocat");
As CommonsWare told already in his answer the entire http client package has been deprecated with Android 23.0.0 build tool version. So we should better use some other API like HttpUrlConnection
or any third part library like Volley
or okHttp
or retrofit
.
But if you still want those all packages; you could add following dependency to your app's module gradle script:
dependencies {
compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
}
and don't forget to add mavenCentral()
in your project gradle script:
allprojects {
repositories {
jcenter()
mavenCentral()
}
}
After adding these; Just synchronize the project with gradle. And you'll be able to import and use these APIs again.
UPDATE:
Thank you @rekire for mentioning that in comment. Here I am adding that too, instead of using above mentioned dependency, you can just add useLibrary 'org.apache.http.legacy'
in your android DSL of module's gradle script. Add it like below:
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
useLibrary 'org.apache.http.legacy'
//rest things...
}
Hope this will help to someone.
You can replace apache's HttpClient with HttpURLConnection