Bogdan Daniel
Bogdan Daniel

Reputation: 2759

With what can I replace http deprecated methods?

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.

Upvotes: 7

Views: 12437

Answers (4)

TheLittleNaruto
TheLittleNaruto

Reputation: 8473

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.

Upvotes: 8

florent champigny
florent champigny

Reputation: 979

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");

Upvotes: 1

Bojan Kseneman
Bojan Kseneman

Reputation: 15668

You can replace apache's HttpClient with HttpURLConnection

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1007584

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

Upvotes: 8

Related Questions