简体   繁体   English

如何使用HttpUrlConnection在android中构建REST客户端

[英]how to build REST client in android using HttpUrlConnection

I want to build an android app that consumes some REST APIs. 我想构建一个消耗一些REST API的Android应用程序。

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. 我正在使用HttpURLConnection进行基本身份验证和GET / PUT一些数据,但我觉得我做错了。 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. 我有两个类ConnectionPUTConnectionGET ,我为每个请求调用,因为每个HttpURLConnection实例用于发出单个请求。

What is the right way to build a REST client with Android using HttpURLConnection? 使用HttpURLConnection在Android上构建REST客户端的正确方法是什么?

This is sample code for calling an Http GET using HttpUrlConnection in Android. 这是在Android中使用HttpUrlConnection调用Http GET的示例代码。

  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. 但我强烈建议您不要重新发明用于为Android应用程序创建REST客户端的轮子,而是尝试使用Retrofit和Volley等适应性强且可靠的库来进行网络连接。

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 有关更多信息,我建议您学习以下有关Retrofit和Volley的文章

Android - Using Volley for Networking Android - 使用Volley进行网络连接

Android -Using Retrofit for Networking Android-使用网络改造

REST client using HttpURLConnection 使用HttpURLConnection的REST客户端

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();

            } 

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM