Android sends https request to server without deprecated methods

My application uses a send request over https, following this original answer . Now some of them apache methods are deprecated. Can someone help me to decide a new approach?

+5
source share
6 answers

To avoid using deprecated methods in your API connection, consider using Retrofit . This is a third-party library that simplifies HTTP communications.

When using Retrofit, you can create an API endpoint interface and use it as a method. The rest of the HTTP request is library managed.

Here is the link to the Retrofit github homepage: http://square.imtqy.com/retrofit/

+5
source

HttpURLConnection is part of the SDK from API 1, you can use the same http://developer.android.com/reference/java/net/HttpURLConnection.html .

// HTTP POST request private void sendPost() throws Exception { //Your server URL String url = "https://selfsolve.apple.com/wcResults.do"; URL obj = new URL(url); HttpsURLConnection con = (HttpsURLConnection) obj.openConnection(); //add reuqest header con.setRequestMethod("POST"); con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); //Request Parameters you want to send String urlParameters = "sn=C02G8416DRJM&cn=&locale=&caller=&num=12345"; // Send post request con.setDoOutput(true);// Should be part of code only for .Net web-services else no need for PHP DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeBytes(urlParameters); wr.flush(); wr.close(); int responseCode = con.getResponseCode(); System.out.println("\nSending 'POST' request to URL : " + url); System.out.println("Post parameters : " + urlParameters); System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader( new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); //print result System.out.println(response.toString()); } 

You can get more detailed information from

+3
source

Please check the function below:

  public String makeServiceCall(String url1, MultipartEntity reqEntity) { try { // http client URL url= new URL(url1); HttpURLConnection httpClient = (HttpURLConnection) url.openConnection(); httpClient.setRequestMethod("POST"); httpClient.setUseCaches(false); httpClient.setDoInput(true); httpClient.setDoOutput(true); httpClient.setRequestProperty("Connection", "Keep-Alive"); httpClient.addRequestProperty("Content-length", reqEntity.getContentLength()+""); OutputStream os = httpClient.getOutputStream(); reqEntity.writeTo(httpClient.getOutputStream()); os.close(); httpClient.connect(); if (httpClient.getResponseCode() == HttpURLConnection.HTTP_OK) { return readStream(httpClient.getInputStream()); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } 
+1
source

Android SDK 23

HttpClient deprecated, you can port your code to HttpURLConnection

Something like that

 URL url = new URL(urlString); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("POST"); connection.setDoInput(true); connection.connect(); 
0
source

if you want to continue using HttpClient for API level 22 and 23 .. add org.apache.http.legacy.jar to the Lib project folder, ull get the .jar file in Android \ sdk \ platform \ android-23 \ optional ..

if ur uses android studio, after copying jar file to lib folder, right click on jar file and click add as library

ur problem will be solved. If you need any help. thanks!

0
source

You can use this method to obtain or publish any purpose. Just use this method to query the server.

 public void RequestToServer() { // String User_id = "h"; AsyncHttpClient client = new AsyncHttpClient(); RequestParams params = new RequestParams(); // params.put("uid", User_id.toString()); client.post("http:// Your Url", params, new AsyncHttpResponseHandler() { @Override public void onSuccess(String s) { super.onSuccess(s); Log.d("Server Response for success :", s); tv.append("service_ReloadSqlDB" + " " + s); } @Override public void onFailure(Throwable throwable) { super.onFailure(throwable); Log.d("Server Response for onFailure ", throwable.toString()); } }); } 

You also need the file jar = android-async-http-1.3.1.jar download this jar and add your android project to the libs folder After that, add this to your build.gradle

 dependencies { compile files('libs/<android-async-http-1.3.1.jar>') } 

Finally, rebuild your project, run the application, get a response from your server.

0
source

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