12

I use ThreadSafeClientConnManager in my app, and a bunch of other classes like HttpStatus, SSLSocketFactory, PlainSocketFactory, SchemeRegistry, etc. But as of API 22 thery're all being marked as deprecated, and I don't see any clear indication as to what replaced them. The documentation jas says to "Please use openConnection() instead. Please visit this webpage for further details", and that doesn't make it very clear what to do. openConnection() just points to the URL class, and the webpage link is from 2011 that talks about the differences between the Apache classes and HttpUtrlConnection. So, does that mean that we're supposed to be useign HttpUrlConnection class from now on? And if that's the case, I thought that it wasn't thread safe (which is why I was using the ThreadSafeClientConnManager).

Could someone please clarify this for me?

user496854
  • 4,639
  • 9
  • 39
  • 74
  • Check out my answer [here](http://stackoverflow.com/a/32202597/2590478). – MidasLefko Jan 13 '16 at 18:12
  • You can still use apache's libraries. Check this answer: http://stackoverflow.com/a/37623038/1727132 – Jehy Jun 03 '16 at 20:43
  • Because there is the alternative HttpURLConnection class which reduces network use and power consumption. Source: https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-apache-http-client – Velizar Hristov Oct 15 '16 at 12:21

2 Answers2

5

I asked something like that about half month ago. As it turns out we have to use only openConnection() instead of the old ones.

I think it's a bit early to change your code, as Lollipop is on a few amount of smartphones, but you should change it so you can cut ahead of time. I got a pretty good idea from here how to a connection Using java.net.URLConnection to fire and handle HTTP requests? and also try to search for "httpurlconnection example"

Also about thread safety this, hope it helps

(I tried to post it as a comment but I don't have enough reputation)

Community
  • 1
  • 1
pap
  • 283
  • 1
  • 6
  • 17
  • Tanks for the post, but I already know how to use HttpUrlConnection. I just need to have someone confirm that it's beasically supposed to replace HttpClient, and what to do about thread safety. Your link confirmed that it's not thread-safe, but I don't see anything that shows how to use it in a threaded environment when I want to make concurrent connections. – user496854 Apr 04 '15 at 16:16
2

Use this HttpUrlConnection as an alternate

public String  performPostCall(String requestURL,
            HashMap<String, String> postDataParams) {

        URL url;
        String response = "";
        try {
            url = new URL(requestURL);

            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(15000);
            conn.setConnectTimeout(15000);
            conn.setRequestMethod("POST");
            conn.setDoInput(true);
            conn.setDoOutput(true);


            OutputStream os = conn.getOutputStream();
            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(os, "UTF-8"));
            writer.write(getPostData(postDataParams));

            writer.flush();
            writer.close();
            os.close();
            int responseCode=conn.getResponseCode();

            if (responseCode == HttpsURLConnection.HTTP_OK) {
                String line;
                BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));
                while ((line=br.readLine()) != null) {
                    response+=line;
                }
            }
            else {
                response="";

                throw new HttpException(responseCode+"");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return response;
    }

....

  private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException{
        StringBuilder result = new StringBuilder();
        boolean first = true;
        for(Map.Entry<String, String> entry : params.entrySet()){
            if (first)
                first = false;
            else
                result.append("&");

            result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
            result.append("=");
            result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
        }

        return result.toString();
    }

source

Community
  • 1
  • 1
Fahim
  • 11,496
  • 4
  • 36
  • 56
  • Thanks for the detailed post, but I already know how to use HttpUrlConnection. I wanted to know if that's what we're supposed to use now (since the API docs are very vague), and what we're supposed to do about it not being thread-safe. What are we supposed to use instead of ThreadSafeClientConnManager? – user496854 Apr 13 '15 at 19:06
  • Read this: http://developer.android.com/about/versions/marshmallow/android-6.0-changes.html – The Dude Nov 08 '15 at 09:53