1

I want to build an android app that consumes some REST APIs.

I'm using HttpURLConnection to make a basic authentication and GET/PUT some data, but I feel that I'm doing it the wrong way. I have a two classes ConnectionPUT and ConnectionGET that I call for every request, since each HttpURLConnection instance is used to make a single request.

What is the right way to build a REST client with Android using HttpURLConnection?

6
  • 3
    There won't be a single "right way" to to this. There are many ways to achieve this, and countless libraries that will help you with this, not to mention the countless SO questions and answers, that cover that topic. Commented Mar 9, 2016 at 8:38
  • 3
    You should be specific about the problem you are having and show us the exact errors. Commented Mar 9, 2016 at 8:39
  • @toKrause thank you, i want those "countless libraries" so i can test them ! Commented Mar 9, 2016 at 8:47
  • @FarhadFaghihi i make the api call several times, i already worked on some web projects , desktop apps with database : we make 1 connection with the database ! so i feel that i'm missing somthing. this is my first project on REST APIs ^^" Commented Mar 9, 2016 at 8:54
  • As I already mentioned, there are already SO questions that cover the topic; and at least mention a lot of available libraries. Commented Mar 9, 2016 at 8:59

2 Answers 2

3

This is sample code for calling an Http GET using HttpUrlConnection in Android.

  URL url;
    HttpURLConnection urlConnection = null;
    try {
        url = new URL("your-url-here");

        urlConnection = (HttpURLConnection) url
                .openConnection();

        InputStream in = urlConnection.getInputStream();

        InputStreamReader isw = new InputStreamReader(in);

        int data = isw.read();
        while (data != -1) {
            char current = (char) data;
            data = isw.read();
            System.out.print(current);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (urlConnection != null) {
            urlConnection.disconnect();


}    
    }

But I strongly recommend that instead of re-inventing the wheel for creating a REST client for your android application, try the well-adapted and reliable libraries like Retrofit and Volley, for networking.

They are highly reliable and tested, and remove all the boilerplate code you have to write for network communication.

For more information, I suggest you to study the following article on Retrofit and Volley

Android - Using Volley for Networking

Android -Using Retrofit for Networking

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

1 Comment

Thank you for your answer !!
0

REST client using HttpURLConnection

try {

        URL url = new URL("YOUR_URL");
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();

        BufferedReader reader = new BufferedReader(
                new InputStreamReader(conn.getInputStream()));

        StringBuffer data= new StringBuffer(1024);
        String tmpdata="";
        while((tmpdata=reader.readLine())!=null) {              

               data.append(tmpdata).append("\n");

           }
        reader.close();

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

        } finally {

         if (conn!= null) {
             conn.disconnect();

            } 

        }

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.