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?

Micky
  • 5,578
  • 7
  • 31
  • 55
Mraimou
  • 175
  • 3
  • 13
  • 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](https://stackoverflow.com/questions/8267928/android-rest-client-sample) and answers, that cover that topic. – toKrause Mar 09 '16 at 08:38
  • 3
    You should be specific about the problem you are having and show us the exact errors. – Farhad Mar 09 '16 at 08:39
  • @toKrause thank you, i want those "countless libraries" so i can test them ! – Mraimou Mar 09 '16 at 08: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 ^^" – Mraimou Mar 09 '16 at 08:54
  • As I already mentioned, there are already [SO](https://stackoverflow.com/questions/8267928/android-rest-client-sample) [questions](https://stackoverflow.com/questions/4945119/best-rest-client-framework-utility-on-android) that cover the topic; and at least mention a lot of available libraries. – toKrause Mar 09 '16 at 08:59
  • @toKrause i'll explore them all, thanks again – Mraimou Mar 09 '16 at 09:07

2 Answers2

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

Farhad
  • 12,178
  • 5
  • 32
  • 60
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();

            } 

        }