Android各大网络请求库的比较及实战



自己学习android也有一段时间了,在实际开发中,频繁的接触网络请求,而网络请求的方式很多,最常见的那么几个也就那么几个。本篇文章对常见的网络请求库进行一个总结。

HttpUrlConnection

最开始学android的时候用的网络请求是HttpUrlConnection,当时很多东西还不知道,但是在android 2.2及以下版本中HttpUrlConnection存在着一些bug,所以建议在android 2.3以后使用HttpUrlConnection,之前使用HttpClient。

在Android 2.2版本之前,HttpClient拥有较少的bug,因此使用它是最好的选择。而在Android 2.3版本及以后,HttpURLConnection则是最佳的选择。它的API简单,体积较小,因而非常适用于Android项目。压缩和缓存机制可以有效地减少网络访问的流量,在提升速度和省电方面也起到了较大的作用。对于新的应用程序应该更加偏向于使用HttpURLConnection,因为在以后的工作当中我们也会将更多的时间放在优化HttpURLConnection上面。

特点
  • 比较轻便,灵活,易于扩展
  • 在3.0后以及4.0中都进行了改善,如对HTTPS的支持
  • 在4.0中,还增加了对缓存的支持
用法
  1. 首先我们需要获取到一个HttpURLConnection实例,一般需要new出一个URL对象,并传入目标网络地址,通过调用openConnection()方法获得HttpURLConnection实例。
  2. 得到该实例后。我们需要设置一下http请求的的方法,这里我们主要研究get和post,默认是使用get方法。get一般用于从服务器获取数据,post一般用于向服务器提交数据,设置请求方法使用函数setRequestMethod(“POST”)进行设置。
  3. 此外可以进行一些请求的限制,比如连接超时的时间等,可以通过setConnectTimeout设置超时时间。
  4. 获取服务器返回的输入流,使用getInputStream方法获取。
  5. 读取内容并处理
  6. 关闭连接,通过调用disconnect方法关闭当前的连接。
    关键代码如下
    使用过程中不要忘记添加权限
1
< uses-permission android:name = "android.permission.INTERNET" />

GET

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
public String get(String urlPath) {
      HttpURLConnection connection =  null ;
      InputStream is =  null ;
      try {
           URL url =  new URL(urlPath);
           //获得URL对象
           connection = (HttpURLConnection) url.openConnection();
           //获得HttpURLConnection对象
           connection.setRequestMethod( "GET" );
           // 默认为GET
           connection.setUseCaches( false );
           //不使用缓存
           connection.setConnectTimeout( 10000 );
           //设置超时时间
           connection.setReadTimeout( 10000 );
           //设置读取超时时间
           connection.setDoInput( true );
           //设置是否从httpUrlConnection读入,默认情况下是true;
           if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                //相应码是否为200
                is = connection.getInputStream();
                //获得输入流
                BufferedReader reader =  new BufferedReader( new InputStreamReader(is));
                //包装字节流为字符流
                StringBuilder response =  new StringBuilder();
                String line;
                while ((line = reader.readLine()) !=  null ) {
                      response.append(line);
                }
                return response.toString();
           }
      catch (Exception e) {
           e.printStackTrace();
      finally {
           if (connection !=  null ) {
                connection.disconnect();
                connection =  null ;
           }
           if (is !=  null ) {
                try {
                     is.close();
                     is =  null ;
                catch (IOException e) {
                     e.printStackTrace();
                }
           }
     }
     return null ;
}

POST

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
private String post(String urlPath, Map<String, String> params) {
      if (params ==  null || params.size() ==  0 ) {
           return get(urlPath);
      }
      OutputStream os =  null ;
      InputStream is =  null ;
      HttpURLConnection connection =  null ;
      StringBuffer body = getParamString(params);
      byte [] data = body.toString().getBytes();
      try {
            URL url =  new URL(urlPath);
            //获得URL对象
            connection = (HttpURLConnection) url.openConnection();
            //获得HttpURLConnection对象
            connection.setRequestMethod( "POST" );
            // 设置请求方法为post
            connection.setUseCaches( false );
            //不使用缓存
            connection.setConnectTimeout( 10000 );
            //设置超时时间
            connection.setReadTimeout( 10000 );
            //设置读取超时时间
            connection.setDoInput( true );
            //设置是否从httpUrlConnection读入,默认情况下是true;
            connection.setDoOutput( true );
            //设置为true后才能写入参数
            connection.setRequestProperty( "Content-Type" "application/x-www-form-urlencoded" );
            connection.setRequestProperty( "Content-Length" , String.valueOf(data.length));
            os = connection.getOutputStream();
            os.write(data);
            //写入参数
            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                 //相应码是否为200
                 is = connection.getInputStream();
                 //获得输入流
                 BufferedReader reader =  new BufferedReader( new InputStreamReader(is));
                 //包装字节流为字符流
                 StringBuilder response =  new StringBuilder();
                 String line;
                 while ((line = reader.readLine()) !=  null ) {
                      response.append(line);
                 }
                 return response.toString();
           }
      catch (Exception e) {
           e.printStackTrace();
      finally {
           //关闭
           if (os !=  null ) {
                try {
                      os.close();
                catch (IOException e) {
                      e.printStackTrace();
                }
           }
           if (is !=  null ) {
                try {
                      is.close();
                catch (IOException e) {
                      e.printStackTrace();
                }
           }
           if (connection !=  null ) {
                connection.disconnect();
                connection =  null ;
           }
      }
      return null ;
}
 
private StringBuffer getParamString(Map<String, String> params) {
      StringBuffer result =  new StringBuffer();
      Iterator<Map.Entry<String, String>> iterator = params.entrySet().iterator();
      while (iterator.hasNext()) {
            Map.Entry<String, String> param = iterator.next();
            String key = param.getKey();
            String value = param.getValue();
            result.append(key).append( '=' ).append(value);
            if (iterator.hasNext()) {
                  result.append( '&' );
            }
      }
      return result;
}

以上代码参考了部分LessCode项目

HttpClient

特点
  • 高效稳定,但是维护成本高昂,故android 开发团队不愿意在维护该库而是转投更为轻便的HttpUrlConnection
用法
  1. HttpClient是一个接口,因此无法直接创建它的实例,一般都是创建一个DefaultHttpClient实例
  2. 如果要发起Get请求,需要创建一个HttpGet对象,并传入请求地址
  3. 如果要发起Post请求,需要创建一个HttpPost对象。并传入请求地址,通过setEntity函数设置请求参数
  4. 调用execute方法,传入HttpGet或者HttpPost实例,执行后返回HttpResponse对象,判断响应状态码
  5. 解析响应结果,通过调用getEntity函数获得一个HttpEntity对象,之后可以通过EntityUtils.toString方法将其转换为字符串

由于在android2.3之后就被HttpUrlConnection取代了,这里也不过多介绍了,不过当初学习它的时候还没接触到其他库,就感觉它好方便,下面简单贴出使用方法

GET

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private String get(String url){
       HttpClient client= null ;
       HttpGet request= null ;
       try {
            client= new DefaultHttpClient();
            request= new HttpGet(url);
            HttpResponse response=client.execute(request);
            if (response.getStatusLine().getStatusCode()== HttpStatus.SC_OK){
                  String result=EntityUtils.toString(response.getEntity(), "UTF-8" );
                  return result;
            }
       catch (IOException e) {
            e.printStackTrace();
       }
       return null ;
}

POST

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
private String post(String url,List<NameValuePair> params){
       HttpClient client= null ;
       HttpPost request= null ;
       try {
            client= new DefaultHttpClient();
            request= new HttpPost(url);
            request.setEntity( new UrlEncodedFormEntity(params, HTTP.UTF_8));
            HttpResponse response=client.execute(request);
            if (response.getStatusLine().getStatusCode()== HttpStatus.SC_OK){
                  String result=EntityUtils.toString(response.getEntity(), "UTF-8" );
                  return result;
            }
       catch (IOException e) {
            e.printStackTrace();
       }
       return null ;
  }

以上代码参考了郭霖《第一行代码》——HttpClient部分

Android Asynchronous Http Client

Android Asynchronous Http Client一看名字就知道它是基于Http Client的,但是呢在安卓中Http Client已经废弃了,所以也不建议使用这个库了。然后仍然有一些可取的内容值得学习,所以这里也介绍一下。

特点
  • 所以请求在子线程中完成,请求回调在调用该请求的线程中完成
  • 使用线程池
  • 使用RequestParams类封装请求参数
  • 支持文件上传
  • 持久化cookie到SharedPreferences,个人感觉这一点也是这个库的重要特点,可以很方便的完成一些模拟登录
  • 支持json
  • 支持HTTP Basic Auth
用法

编写一个静态的HttpClient

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package cn.edu.zafu.http;
 
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
 
/**
  * Created by lizhangqu on 2015/5/7.
  */
  public class TestClient {
       private static final String BASE_URL =  "http://121.41.119.107/" ;
 
       private static AsyncHttpClient client =  new AsyncHttpClient();
 
       public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
             client.get(getAbsoluteUrl(url), params, responseHandler);
       }
 
       public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
             client.post(getAbsoluteUrl(url), params, responseHandler);
       }
 
       private static String getAbsoluteUrl(String relativeUrl) {
             return BASE_URL + relativeUrl;
       }
}

调用get或者post方法

参数通过RequestParams传递,没有参数则传递null

1
2
RequestParams  params =  new RequestParams();
params.put( "" , "" );

如果要保存cookie,在发起请求之前调用以下代码

1
2
PersistentCookieStore myCookieStore =  new PersistentCookieStore( this );
client.setCookieStore(myCookieStore);

之后请求所得到的cookie都会自动持久化

如果要自己添加cookie,则调用以下代码

1
2
3
4
5
BasicClientCookie newCookie =  new BasicClientCookie( "cookiesare" "awesome" );
newCookie.setVersion( 1 );
newCookie.setDomain( "mydomain.com" );
newCookie.setPath( "/" );
myCookieStore.addCookie(newCookie);

使用
在回调函数中处理返回结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
private void get(){
         TestClient.get( "test/index.php" null new AsyncHttpResponseHandler() {
             @Override
             public void onSuccess( int statusCode, Header[] headers,  byte [] responseBody) {
 
             }
 
             @Override
             public void onFailure( int statusCode, Header[] headers,  byte [] responseBody, Throwable error) {
 
             }
         });
     }
     private void post(){
         RequestParams params =  new RequestParams();
         params.put( "user" , "asas" );
         params.put( "pass" , "12121" );
         params.put( "time" , "1212121" );
         TestClient.post( "test/login.php" , params,  new AsyncHttpResponseHandler() {
             @Override
             public void onSuccess( int statusCode, Header[] headers,  byte [] responseBody) {
 
             }
 
             @Override
             public void onFailure( int statusCode, Header[] headers,  byte [] responseBody, Throwable error) {
 
             }
         });
     }

以上代码参考了Android Asynchronous Http Client官方实例

Volley

既然在android2.2之后不建议使用Http Client,那么有没有一个库是android2.2及以下版本使用Http Client,而android2.3及以上版本使用HttpUrlConnection的呢,答案是肯定的,就是Volley,它是android开发团队在2013年Google I/O大会上推出了一个新的网络通信框架

Volley可以说是把AsyncHttpClient和Universal-Image-Loader的优点集于了一身,既可以像AsyncHttpClient一样非常简单地进行HTTP通信,也可以像Universal-Image-Loader一样轻松加载网络上的图片。除了简单易用之外,Volley在性能方面也进行了大幅度的调整,它的设计目标就是非常适合去进行数据量不大,但通信频繁的网络操作,而对于大数据量的网络操作,比如说下载文件等,Volley的表现就会非常糟糕

特点
  1. Volley的优势在于处理小文件的http请求;
  2. 在Volley中也是可以使用Okhttp作为传输层
  3. Volley在处理高分辨率的图像压缩上有很好的支持;
  4. NetworkImageView在GC的使用模式上更加保守,在请求清理上也更加积极,networkimageview仅仅依赖于强大的内存引用,并当一个新请求是来自ImageView或ImageView离开屏幕时 会清理掉所有的请求数据。
用法
  1. 创建一个RequestQueue对象。
  2. 创建一个Request对象。
  3. 将Request对象添加到RequestQueue里面。

下面一步一步来学习其用法

GET

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private void get(){
        RequestQueue queue= Volley.newRequestQueue(getApplicationContext());
        String url= "http://121.41.119.107/test/index.php" ;
        StringRequest request= new StringRequest(url,  new Response.Listener&lt;String&gt;() {
            @Override
            public void onResponse(String response) {
                Log.d( "TAG" ,response);
            }
        },  new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
 
            }
        });
        queue.add(request);
    }

POST
通过指定请求方法为Request.Method.POST使其成为post请求,然后重新getParams方法设置请求参数。当发出POST请求的时候,Volley会尝试调用StringRequest的父类——Request中的getParams()方法来获取POST参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
private void post() {
        RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
        String url =  "http://121.41.119.107/test/login.php" ;
        StringRequest request =  new StringRequest(Request.Method.POST, url,  new Response.Listener&lt;String&gt;() {
            @Override
            public void onResponse(String response) {
                Log.d( "TAG" , response);
            }
        },  new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
 
            }
        }) {
            //重写getParams方法设置参数
            @Override
            protected Map&lt;String, String&gt; getParams()  throws AuthFailureError {
                Map&lt;String, String&gt; params =  new HashMap&lt;String, String&gt;();
                params.put( "user" "asas" );
                params.put( "pass" "12121" );
                params.put( "time" "1212121" );
                return params;
            }
        };
        queue.add(request);
    }

加载图片
加载图像的方法和前面类似,只不过不在是StringRequest而是ImageRequest。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
private void getImage() {
        RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
        String url =  "https://www.baidu.com/img/bdlogo.png" ;
        //第三第四个参数分别用于指定允许图片最大的宽度和高度,如果指定的网络图片的宽度或高度大于这里的最大值,则会对图片进行压缩,指定成0的话就表示不管图片有多大,都不会进行压缩。
        //第五个参数就是ImageView里中的属性ScaleType
        //第六个参数用于指定图片的颜色属性
        ImageRequest request =  new ImageRequest(url,  new Response.Listener&lt;Bitmap&gt;() {
            @Override
            public void onResponse(Bitmap response) {
                ImageView iv= (ImageView) findViewById(R.id.iv);
                iv.setImageBitmap(response);
            }
        },  0 0 , ImageView.ScaleType.CENTER, Bitmap.Config.ARGB_8888,  new Response.ErrorListener() {
 
            @Override
            public void onErrorResponse(VolleyError error) {
 
            }
        });
        queue.add(request);
    }

其实加载图片的功能还远远不止这些,使用ImageLoader可以实现对图片的缓存,还可以过滤重复链接,避免发送重复的请求
ImageLoader的使用方法概括为以下几步
1. 创建一个RequestQueue对象。
2. 创建一个ImageLoader对象。
3. 获取一个ImageListener对象。
4. 调用ImageLoader的get()方法加载网络上的图片。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//继承ImageCache,使用LruCache实现缓存
    public class BitmapCache  implements ImageLoader.ImageCache {
        private LruCache&lt;String, Bitmap&gt; mCache;
        public BitmapCache() {
            int maxSize =  10 1024 1024 ;
            mCache =  new LruCache&lt;String, Bitmap&gt;(maxSize) {
                @Override
                protected int sizeOf(String key, Bitmap bitmap) {
                    return bitmap.getRowBytes() * bitmap.getHeight();
                }
            };
        }
        @Override
        public Bitmap getBitmap(String url) {
            return mCache.get(url);
        }
        @Override
        public void putBitmap(String url, Bitmap bitmap) {
            mCache.put(url, bitmap);
        }
 
    }
    private void getImageByImageLoader() {
        ImageView iv= (ImageView) findViewById(R.id.iv);
        RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
        String url =  "https://www.baidu.com/img/bdlogo.png" ;
        ImageLoader loader= new ImageLoader(queue, new BitmapCache() );
        // 第一个参数指定用于显示图片的ImageView控件
        // 第二个参数指定加载图片的过程中显示的图片
        // 第三个参数指定加载图片失败的情况下显示的图片
        ImageLoader.ImageListener listener=ImageLoader.getImageListener(iv,R.mipmap.ic_launcher,R.mipmap.ic_launcher);
        // 调用ImageLoader的get()方法来加载图片
        // 第一个参数就是图片的URL地址
        // 第二个参数则是刚刚获取到的ImageListener对象
        // 如果想对图片的大小进行限制,也可以使用get()方法的重载,指定图片允许的最大宽度和高度,即通过第三第四个参数指定
        loader.get(url,listener);
    }

最后,Volley提供了一种自定义ImageView来加载图片,其使用方法可概括为
1. 创建一个RequestQueue对象。
2. 创建一个ImageLoader对象。
3. 在布局文件中添加一个NetworkImageView控件。
4. 在代码中获取该控件的实例。
5. 设置要加载的图片地址。

我们在布局中申明该控件

1
2
3
4
5
6
< com.android.volley.toolbox.NetworkImageView
         android:id = "@+id/network_image_view"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:layout_centerInParent = "true"
         />

在程序中实现加载

1
2
3
4
5
6
7
8
public void networkImageView(){
         RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
         ImageLoader loader= new ImageLoader(queue, new BitmapCache() );
         NetworkImageView niv= (NetworkImageView) findViewById(R.id.network_image_view);
         niv.setDefaultImageResId(R.mipmap.ic_launcher); //设置加载中显示的图片
         niv.setErrorImageResId(R.mipmap.ic_launcher); //设置加载失败时显示的图片
         niv.setImageUrl( "https://www.baidu.com/img/bdlogo.png" ,  loader);//设置目标图片的URL地址
     }

自定义Request

在实际应用中,往往需要将http请求与json进行集成,而Volley正恰恰支持这样的方式,不过需要我们自己自定义Request,这里我们使用google的Gson库进行集成。
1. 继承Request类
2. 重写parseNetworkResponse,实现json与实体类转换,由于实体类未定,所以采用泛型

下文用到的json字符串如下

1
{ "name" : "lizhangqu" , "age" : 16 }
  • 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package cn.edu.zafu.http;
 
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.HttpHeaderParser;
import com.google.gson.Gson;
 
import java.io.UnsupportedEncodingException;
 
/**
  * Created by lizhangqu on 2015/5/7.
  */
public class GsonRequest&lt;T&gt;  extends Request&lt;T&gt; {
 
     private final Response.Listener&lt;T&gt; mListener;
 
     private Gson mGson;
 
     private Class&lt;T&gt; mClass;
 
     public GsonRequest( int method, String url, Class&lt;T&gt; clazz, Response.Listener&lt;T&gt; listener,
                        Response.ErrorListener errorListener) {
         super (method, url, errorListener);
         mGson =  new Gson();
         mClass = clazz;
         mListener = listener;
     }
 
     public GsonRequest(String url, Class&lt;T&gt; clazz, Response.Listener&lt;T&gt; listener,
                        Response.ErrorListener errorListener) {
         this (Method.GET, url, clazz, listener, errorListener);
     }
 
     @Override
     protected Response&lt;T&gt; parseNetworkResponse(NetworkResponse response) {
         try {
             String jsonString =  new String(response.data,
                     HttpHeaderParser.parseCharset(response.headers));
             return Response.success(mGson.fromJson(jsonString, mClass),
                     HttpHeaderParser.parseCacheHeaders(response));
         catch (UnsupportedEncodingException e) {
             return Response.error( new ParseError(e));
         }
     }
 
     @Override
     protected void deliverResponse(T response) {
         mListener.onResponse(response);
     }
 
}

编写测试实体类,两个字段一个name一个age

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package cn.edu.zafu.http;
 
/**
  * Created by lizhangqu on 2015/5/7.
  */
public class Person {
     private String name;
     private int age;
 
     public String getName() {
         return name;
     }
 
     public void setName(String name) {
         this .name = name;
     }
 
     public int getAge() {
         return age;
     }
 
     public void setAge( int age) {
         this .age = age;
     }
 
     @Override
     public String toString() {
         return "Person{" +
                 "name='" + name + '\ '' +
                 ", age=" + age +
                 '}' ;
     }
}

调用方法和StringRequest是一样的。如下所示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private void json(){
       RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
       String url =  "http://121.41.119.107/test/index.php" ;
       GsonRequest&lt;Person&gt; request= new GsonRequest&lt;Person&gt;(url, Person. class new Response.Listener&lt;Person&gt;() {
           @Override
           public void onResponse(Person response) {
               Log.d( "TAG" ,response.toString());
           }
       },  new Response.ErrorListener() {
           @Override
           public void onErrorResponse(VolleyError error) {
 
           }
       });
       queue.add(request);
   }

以上代码参考了郭霖三篇Volley博客文章,分别为
Android Volley完全解析(一),初识Volley的基本用法
Android Volley完全解析(二),使用Volley加载网络图片
Android Volley完全解析(三),定制自己的Request

okHttp

okhttp 是一个 Java 的 HTTP+SPDY 客户端开发包,同时也支持 Android。需要Android 2.3以上。

特点
  • OKHttp是Android版Http客户端。非常高效,支持SPDY、连接池、GZIP和 HTTP 缓存。
  • 默认情况下,OKHttp会自动处理常见的网络问题,像二次连接、SSL的握手问题。
  • 如果你的应用程序中集成了OKHttp,Retrofit默认会使用OKHttp处理其他网络层请求。
  • 从Android4.4开始HttpURLConnection的底层实现采用的是okHttp.
用法
  1. 新建一个OkHttpClient对象
  2. 通过Request.Builder对象新建一个Request对象
  3. 返回执行结果

GET

1
2
3
4
5
6
7
8
9
10
11
12
13
14
private String get(String url) {
         OkHttpClient client =  new OkHttpClient();
         Request request =  new Request.Builder()
                 .url(url)
                 .build();
         Response response =  null ;
         try {
             response = client.newCall(request).execute();
             return response.body().string();
         catch (IOException e) {
             e.printStackTrace();
         }
         return null ;
     }

POST

POST需要使用RequestBody对象,之后再构建Request对象时调用post函数将其传入即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
private String post(String url) {
         OkHttpClient client =  new OkHttpClient();
 
         RequestBody formBody =  new FormEncodingBuilder()
                 .add( "user" "Jurassic Park" )
                 .add( "pass" "asasa" )
                 .add( "time" "12132" )
                 .build();
         Request request =  new Request.Builder()
                 .url(url)
                 .post(formBody)
                 .build();
         Response response =  null ;
         try {
             response = client.newCall(request).execute();
             return response.body().string();
         catch (IOException e) {
             e.printStackTrace();
         }
         return null ;
     }

此外,post的使用方法还支持文件等操作,具体使用方法有兴趣的可以自行查阅

对Gson的支持

okHttp还自带了对Gson的支持

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private Person gson(String url){
       OkHttpClient client =  new OkHttpClient();
       Gson gson =  new Gson();
       Request request =  new Request.Builder()
               .url(url)
               .build();
       Response response =  null ;
       try {
           response = client.newCall(request).execute();
           Person person = gson.fromJson(response.body().charStream(), Person. class );
           return person;
       catch (IOException e) {
           e.printStackTrace();
       }
       return null ;
   }

异步操作

以上的两个例子必须在子线程中完成,同时okHttp还提供了异步的方法调用,通过使用回调来进行异步调用,然后okHttp的回调依然不在主线程中,因此该回调中不能操作UI

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
private void getAsync(String url) {
        OkHttpClient client =  new OkHttpClient();
        Request request =  new Request.Builder()
                .url(url)
                .build();
        Response response =  null ;
 
        client.newCall(request).enqueue( new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
 
            }
 
            @Override
            public void onResponse(Response response)  throws IOException {
                String result = response.body().string();
                Toast.makeText(getApplicationContext(),result,Toast.LENGTH_SHORT).show();
                //不能操作ui,回调依然在子线程
                Log.d( "TAG" , result);
            }
        });
 
    }

okHttp的使用还有很多内容,这里也不过多介绍,更多内容,参考官方网址

Retrofit

特点
  1. 性能最好,处理最快
  2. 使用REST API时非常方便;
  3. 传输层默认就使用OkHttp;
  4. 支持NIO;
  5. 拥有出色的API文档和社区支持
  6. 速度上比volley更快;
  7. 如果你的应用程序中集成了OKHttp,Retrofit默认会使用OKHttp处理其他网络层请求。
  8. 默认使用Gson
使用

Retrofit支持同步和异步两种方式,在使用时,需要将请求地址转换为接口,通过注解来指定请求方法,请求参数,请求头,返回值等信息。还是使用之前的person的那段json值,get请求到服务器后从数据库查询数据,返回值为查询到的数据,post请求向服务器提交一条数据,返回值为提交的数据。
首先完成请求所用的service,是一个interface,完全通过注解完成配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package cn.edu.zafu.http;
 
import retrofit.Callback;
import retrofit.http.Field;
import retrofit.http.FormUrlEncoded;
import retrofit.http.GET;
import retrofit.http.Headers;
import retrofit.http.POST;
import retrofit.http.Path;
import retrofit.http.Query;
 
/**
  * Created by lizhangqu on 2015/5/11.
  */
public interface PersonService {
     @Headers ({
             "Cache-Control: max-age=640000" ,
             "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"
     })
     //通过注解设置请求头
     @GET ( "/{test}/rest.php" )
     //设置请求方法为get,相对路径为注解内内容,其中{test}会被@Path注解指定内容替换
     Person getPerson( @Path ( "test" ) String dir, @Query ( "name" ) String name);
     //@Query用于指定参数
 
     @FormUrlEncoded
     //urlencode
     @POST ( "/test/rest1.php" )
     //post提交
     Person updatePerson( @Field ( "name" ) String name, @Field ( "age" int age);
     //@Field提交的域
 
     @POST ( "/test/rest1.php" )
     void updatePerson( @Field ( "name" ) String name, @Field ( "age" int age, Callback&lt;Person&gt; callback);
     //异步回调,不能指定返回值
}

GET
使用时,通过RestAdapter的实例获得一个接口的实例,其本质是动态代理,注意含有返回值的方法是同步的,不能UI线程中调用,应该在子线程中完成

1
2
3
4
5
6
RestAdapter restAdapter =  new RestAdapter.Builder()
                         .setEndpoint( "http://121.41.119.107" )
                         .build();
                 PersonService personService=restAdapter.create(PersonService. class );
                 Person person=personService.getPerson( "test" , "zhangsan" );
                 Log.d( "TAG" ,person.toString());

POST

POST的调用同Get,获得adapter后获得一个代理对象,然后通过这个代理对象进行网络请求

1
2
Person person1=personService.updatePerson( "lizhangqu" 12 );
Log.d( "TAG" ,person1.toString());

异步请求

如果要使用异步请求,需要将接口中的方法返回值修改会void,再加入回调参数Callback,就如PersonService中第三个方法一样,请求完成后会回调该callback对象的success或者fail方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
RestAdapter restAdapter =  new RestAdapter.Builder()
                 .setEndpoint( "http://121.41.119.107" )
                 .build();
         PersonService personService=restAdapter.create(PersonService. class );
         personService.updatePerson( "lizhangqu" , 23 new Callback&lt;Person&gt;() {
             @Override
             public void success(Person person, Response response) {
                 Log.d( "TAG" , person.toString());
             }
 
             @Override
             public void failure(RetrofitError error) {
 
             }
         });

Retrofit的使用还有很多内容,剩下的就留给各位读者自行去发现了,而其官网页提供了及其详细的说明。下面提供官方网址

retrofit官网示例

这个库里面有很多精华的内容,建议各位仔细的阅读下官方的文档。

RoboSpice

见之前写的一篇博文

RoboSpice:android异步网络库简单用法

总结

网络请求库多种多样,最终其本质思想是一致的,要学会融汇贯通,还是要fucking the source code。由于本篇文章已经过长,所以图片的网络加载准备另开一篇博客进行整理。

源码下载

源码下载

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值