With what can I replace http deprecated methods?

耗尽温柔 提交于 2019-11-30 14:35:46

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:

With respect to the tutorial, either:

  • Use tutorials that do not use a deprecated HTTP API, or
  • Port the tutorial to use Apache's repackaged HttpClient for Android, or
  • Ignore the deprecation warnings

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

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");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!