1

I'm trying to find how to do some Http Request in Android 6.0,
Since Android go to the 6.0 version, I can't find anything to help me with the 6.0 changes.

I just wanna do some HTTP request to an REST Api without any dependencies

I've done this but its return me 405 instead of 200.

@Override
protected Object doInBackground(Object[] params) {
    public String rest_Url = "https://balblabla" ;
    HttpURLConnection client = null;
    try {
        URL url = new URL(rest_Url);
        client = (HttpURLConnection) url.openConnection();
        if (client != null){
            client.setRequestMethod("GET");
           // client.setRequestProperty("Accept-Encoding", "identity");
            client.setDoOutput(true);
        }

        OutputStream outputPost = new BufferedOutputStream(client.getOutputStream());
        outputPost.flush();
        if (outputPost!= null){outputPost.close();}

        int responseCode = client.getResponseCode();
        if (responseCode != 200){
            String failure = "ECHEC";
            System.out.println(failure);
        }else {
            String success = "BRAVO";
            System.out.println(success);
        }

    } catch (IOException e) {e.printStackTrace();}

    finally {
        if(client != null){
            client.disconnect();
        }
    }

    return null;`

Thanks for your answers.

CSecchi
  • 13
  • 3
  • 5
    https://stackoverflow.com/questions/32949626/org-apache-http-entity-fileentity-is-deprecated-in-android-6-marshmallow – CommonsWare Jun 29 '16 at 14:41

2 Answers2

1

You have to add third party library .There is no other option I guess even i faced same problem. Apache

Vinayak Shedgeri
  • 2,222
  • 1
  • 21
  • 24
1

Since apache.http is deprecated from sdk 23 you should use some networking library.

You can either use Apache Commons independent package for Android.

OR

There are some great libraries available to help you make HTTP requests in Android.

You can use Android's Volley library

or you can use OkHttp.

Read this for better understanding.

Community
  • 1
  • 1
Ashish Ranjan
  • 5,523
  • 2
  • 18
  • 39