With what can I replace http deprecated methods?
Asked Answered
A

4

7

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

ArrayList<NameValuePair> dataToSend = new ArrayList<>();
dataToSend.add(new BasicNameValuePair("name", user.name));
dataToSend.add(new BasicNameValuePair("age", user.age));

HttpParams httpRequestParams = new BasicHttpParams();
HttpConnectionParamas.setConnectionTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpConnectionParamas.setSoTimeout(httpRequestParams, CONNECTION_TIMEOUT);

HttpClient client = new DefaultHttpClient(httpRequestParams);
HttpPost post = new HttpPost(SERVER_ADDRESS + "Register.php");

try{
    post.setEntity(new UrlEncodedFormEntity(dataToSend));
    client.execute(post);
}catch (Exception e){
    e.printStackTrace();
}

and another POST method that is returning a result

    HttpResponse httpResponse = client.execute(post);

    HttpEntity entity = httpResponse.getEntity();
    String result = EntityUtils.toString(entity);
    JSONObject jObject = new JSONObject(result);

I found that I can replace NameValuePair with

ContentValues values = new ContentValues();
values.put("name", user.name);
values.put("age", user.age + "");

but I have no idea about the others.

Arnuad answered 1/5, 2015 at 21:58 Comment(1)
In here has a answer to your problem. ThanksFlam
U
8

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
Unintelligible answered 1/5, 2015 at 22:2 Comment(0)
B
8

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.

Bucolic answered 9/9, 2015 at 7:50 Comment(2)
I didn't check it but I would guess that useLibrary 'org.apache.http.legacy' would work too.Checkerboard
@ridoy Try my updated answer. More relevant than before.Bucolic
I
1

You can replace apache's HttpClient with HttpURLConnection

Intercourse answered 1/5, 2015 at 22:2 Comment(0)
B
1

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");
Bisset answered 1/5, 2015 at 22:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.