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.

1

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.