-1

In API 22, everything has seemingly been deprecated. I am able to post to a server below API 22, but how is it done above API 22? I can't find anything, because it is still too new. Here is what I have working below API 22

public static void sendData(final String name,  final String from, final String message)
{


    Thread thread = new Thread(new Runnable(){
            @Override
            public void run(){
                String content = "";
                //code to do the HTTP request
                try
                {               
                    /* Sends data through a HTTP POST request */
                    DefaultHttpClient httpClient = new DefaultHttpClient();
                    HttpPost httpPost = new HttpPost("http://mywebsite.com/contact.php");
                    List <NameValuePair> params = new ArrayList <NameValuePair>();
                    params.add(new BasicNameValuePair("cf_name", name));

                    params.add(new BasicNameValuePair("cf_email", from));

                    params.add(new BasicNameValuePair("cf_message", message));
                    httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));

                    /* Reads the server response */
                    HttpResponse response = httpClient.execute(httpPost);
                    InputStream in = response.getEntity().getContent();

                    StringBuffer sb = new StringBuffer();
                    int chr;
                    while ((chr = in.read()) != -1)
                    {
                        sb.append((char) chr);
                    }
                    content = sb.toString();
                    in.close();

                    /* If there is a response, display it */
                    if (!content.equals(""))
                    {
                        Log.i("HTTP Response", content);
                    }
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        });
    thread.start();


}
japk
  • 619
  • 6
  • 10

1 Answers1

0

In the end I tried many solutions, the only one that worked was this answer from Fahim

Community
  • 1
  • 1
japk
  • 619
  • 6
  • 10