Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android send https post request to the server without deprecated methods

In my app is used send request by https by following this source answer. Now some of them apache methods deprecated. Can anyone help me in order solve in a new approach?

like image 847
nAkhmedov Avatar asked Sep 30 '15 04:09

nAkhmedov


People also ask

How do you send a POST request on Android?

build(); Request request = new Request. Builder() . url("https://yourdomain.org/callback.php") // The URL to send the data to . post(formBody) .

How do I send a https post request?

Under HTTP request settings: Enter a URL in the field provided. Select either Use HTTP GET or Use HTTP POST. Enter HTTPS instead of HTTP in the URL to send the information using HTTPS.

What is HttpURLConnection in Android?

HttpsURLConnection. HttpsURLConnection. HttpsURLConnection extends HttpURLConnection with support for https-specific features. The abstract class URLConnection is the superclass of all classes that represent a communications link between the application and a URL.


2 Answers

To avoid using deprecated methods in API connectivity, think about using Retrofit. It's a third party library which makes HTTP communication much simpler.

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

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

like image 123
markView Avatar answered Sep 27 '22 18:09

markView


HttpURLConnection is part of SDK from API 1, you can use 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());

}

More details you can get from

  1. http://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/
  2. http://syntx.io/how-to-send-an-http-request-from-android-using-httpurlconnection/
like image 31
Chanchal Shelar Avatar answered Sep 27 '22 19:09

Chanchal Shelar