With what can I replace http deprecated methods?

后端 未结 4 862
迷失自我
迷失自我 2021-01-02 15:15

I was following a tutorial and I got to a point where a lot of the code is deprecated.

ArrayList dataToSend = new ArrayList<>();
d         


        
相关标签:
4条回答
  • 2021-01-02 15:42

    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 Java
    • third-party ones, like OkHttp
    • Apache's repackaged HttpClient for Android

    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
    0 讨论(0)
  • 2021-01-02 15:47

    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");
    
    0 讨论(0)
  • 2021-01-02 15:56

    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.

    0 讨论(0)
  • 2021-01-02 16:02

    You can replace apache's HttpClient with HttpURLConnection

    0 讨论(0)
提交回复
热议问题