What can I replace legacy http methods with?

I followed the tutorial and I got to the point that a lot of code is out of date.

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 returning the 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 others.

+6
source share
4 answers

I found that I can replace NameValuePair with

Not really.

but I have no idea about others

The entire HttpClient API that comes with Android is deprecated. The solution is to use another HTTP client:

In relation to the textbook:

  • Use tutorials that don't use the legacy HTTP API, or
  • Port a tutorial on using Apache repackaged with HttpClient for Android, or
  • Ignore obsolescence warnings
+8
source

As CommonsWare already said in its answer, the entire http client package is deprecated with the version of the tool for Android 23.0.0. Therefore, we are better off using some other API, such as HttpUrlConnection or any third- HttpUrlConnection library, such as Volley or okHttp or retrofit .

But if you still need all the packages; you can add the following dependency to your gradle script application module:

 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() to your gradle script project:

 allprojects { repositories { jcenter() mavenCentral() } } 

After adding these; Just sync your project with gradle. And you can import and use these APIs again.

UPDATE:

Thanks to @rekire for mentioning this in a comment. Here I also add that instead of using the above dependency, you can simply add useLibrary 'org.apache.http.legacy' to your Android DSL module for gradle script. Add it as shown below:

 android { compileSdkVersion 23 buildToolsVersion "23.0.2" useLibrary 'org.apache.http.legacy' //rest things... } 

Hope this helps someone.

+8
source

You can replace apache with HttpClient HttpURLConnection

+1
source

You should try Retrofit, it’s easier to use this library instead of making HTTP requests. It was created to simplify the communication between java and the REST API.

square.imtqy.com/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 to the generated GitHubService makes an HTTP request to the remote web server.

 List<Repo> repos = service.listRepos("octocat"); 
+1
source

Source: https://habr.com/ru/post/986386/


All Articles