安卓常用网络请求框架XUtils、OkHttp、Volley、Retrofit

    xiaoxiao2022-06-26  120

    1.xutils 此框架庞大而周全,这个框架可以网络请求,同时可以图片加载,又可以数据存储,又可以 View 注解,使用这种框架很方便,这样会使得你整个项目对它依赖性太强,万一以后这个库不维护了,或者中间某个模块出问题了,这个影响非常大,所以在项目开发时,一般会更喜欢选择专注某一领域的框架。

    使用案例:

    RequestParams params = new RequestParams("URL"); params.addBodyParameter("参数", "值"); x.http().get(params, new Callback.CommonCallback<String>() { public void onSuccess(String result) {...} //请求成功 public void onError(Throwable ex, boolean isOnCallback) {...} //客户端出错,没有连接上网络,或者请求完毕报错 public void onCancelled(CancelledException cex) {...} //用户取消 public void onFinished() {...} //请求结束 必备。在此关闭dialog });

    常见问题:

    当使用网络请求或者是xutils时出现以下错误时: Error:(33, 35) 错误: 无法访问HttpRequestBase 找不到org.apache.http.client.methods.HttpRequestBase的类文件 问题的原因是: android 6.0(api 23) SDK中对Android的网络请求强制使用HttpUrlConnection,并且SDK中也已经移除了HttpClient 解决方案很简单 (1)、当是使用eclipse尽心编辑时: 在libs中加入架包org.apache.http.legacy.jar,这个加包是6.0的sdk

    (2)、在对应model的gradle中加入:

    android {useLibrary 'org.apache.http.legacy'}

    2.OkHttp(建议使用) Android 开发中是可以直接使用现成的api进行网络请求的,就是使用HttpClient、HttpUrlConnection 进行操作,目前HttpClient 已经被废弃,而 android-async-http 是基于HttpClient的,可能也是因为这个原因作者放弃维护。 而OkHttp是Square公司开源的针对Java和Android程序,封装的一个高性能http请求库,它的职责跟HttpUrlConnection 是一样的,支持 spdy、http 2.0、websocket ,支持同步、异步,而且 OkHttp 又封装了线程池,封装了数据转换,封装了参数使用、错误处理等,api使用起来更加方便。可以把它理解成是一个封装之后的类似HttpUrlConnection的东西,但是在使用的时候仍然需要自己再做一层封装,这样才能像使用一个框架一样更加顺手。 使用案例(这里是个人的代码,可以参考下):

    //这里需要使用ok http的post功能,将手机号与密码上传 OkHttpClient okHttpClient=new OkHttpClient(); okHttpClient.setCookieHandler(new CookieManager(null, CookiePolicy.ACCEPT_ALL)); //这里修改成你要请求的Url String mBaseUrl="http://47.101.204.40:8080/app_housework/user_login"; FormEncodingBuilder requestBodyBuilder=new FormEncodingBuilder(); //构造你自己需要的 requestBody RequestBody requestBody=requestBodyBuilder.add("phoneNumber",phoneNum).add("password",psw).build(); Request.Builder builder=new Request.Builder(); Request request = builder.url(mBaseUrl).post(requestBody).post(requestBody).build(); Call call=okHttpClient.newCall(request); //返回信息 call.enqueue(new Callback() { @Override public void onFailure(Request request, IOException e) { //输出出错信息 L.e("onFailure:"+e.getMessage()); e.printStackTrace(); } @Override public void onResponse(final Response response) throws IOException { // Gson gson=new Gson(); final String res=response.body().string(); Log.i("denglu", res); //需捕获异常 try{ Gson gson=new Gson(); Type type = new TypeToken<Map<String, Object>>(){}.getType(); Map<String, Object> sList = gson.fromJson(res, type); final String result= sList.get("message").toString(); Log.i("res",result); if(res.equals("unexisted")){ toast("该账号尚未注册!"); } runOnUiThread(new Runnable() { @Override public void run() { SharedPreferences sp=getSharedPreferences("data", MODE_PRIVATE); long errorTime=sp.getLong("errorTime", 0L);//输入错误时的时间 long recentTime=System.currentTimeMillis();//获取当前时间 if (result.equals("success")&&recentTime-errorTime>WAIT_TIME) { //成功登陆 跳转 LoginActivity.this.startActivity(intentindex); LoginActivity.this.finish(); } else { //登录失败 } } } );//线程结束 }catch(Exception e){ e.printStackTrace(); toast("服务器返回错误"); } } });//正确返回结束

    常见问题(1): 已经导入最新的包,但是okhttp的Request.Builder().url().buid()的Builder()不存在怎么办?

    OkHttpClient okHttpClient = new OkHttpClient(); final okhttp3.Request request = new okhttp3.Request.Builder().url(url) .build();

    解决:你需要前面加上包名就可以了

    常见问题(2): 运行到response.body().string()一步时抛异常 java.lang.IllegalStateException: closed 具体的意思就是

    The IllegalStateException arises because the HttpConnection seems to be closed when trying to use it. Could it be because you are calling twice the method response.body()?

    调用response.body().string()的时候数据流已经关闭 解决:用一个变量存储你要打印的就可以,不要直接打印出来

    3.Volley Volley是Google官方出的一套小而巧的异步请求库,该框架封装的扩展性很强,支持HttpClient、HttpUrlConnection, 甚至支持OkHttp,而且Volley里面也封装了ImageLoader,所以如果你愿意你甚至不需要使用图片加载框架,不过这块功能没有一些专门的图片加载框架强大,对于简单的需求可以使用,稍复杂点的需求还是需要用到专门的图片加载框架。Volley也有缺陷,比如不支持post大数据,所以不适合上传文件。不过Volley设计的初衷本身也就是为频繁的、数据量小的网络请求而生。 使用案例: 进行了以下四个步操作:

    创建一个RequestQueue对象。创建一个StringRequest对象。将StringRequest对象添加到RequestQueue里面。清单文件加入网络访问权限 StringRequest stringRequest = new StringRequest("http://www.baidu.com", new Response.Listener<String>() { @Override public void onResponse(String response) { Log.d("TAG", response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e("TAG", error.getMessage(), error); } });

    加入队列

    mQueue.add(stringRequest);

    加入网络权限

    <uses-permission android:name="android.permission.INTERNET" />

    请求百度网址,你会在Logcat打印出消息(消息是网页代码,不用在意) 你也可以加上自己想传的参数,例如这样:

    StringRequest stringRequest = new StringRequest(Method.POST, url, listener, errorListener) { @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String, String> map = new HashMap<String, String>(); map.put("params1", "value1"); map.put("params2", "value2"); return map; } };

    返回错误类型有以下几种: AuthFailureError:如果在做一个HTTP的身份验证,可能会发生这个错误。 NetworkError:Socket关闭,服务器宕机,DNS错误都会产生这个错误。 NoConnectionError:和NetworkError类似,这个是客户端没有网络连接。 ParseError:在使用JsonObjectRequest或JsonArrayRequest时,如果接收到的JSON是畸形,会产生异常。 SERVERERROR:服务器的响应的一个错误,最有可能的4xx或5xx HTTP状态代码。 TimeoutError:Socket超时,服务器太忙或网络延迟会产生这个异常。默认情况下,Volley的超时时间为2.5秒。如果得到这个错误可以使用RetryPolicy。 问题可以根据错误的类型查询相关文章

    4.Retrofit Retrofit是Square公司出品的默认基于OkHttp封装的一套RESTful网络请求框架,RESTful是目前流行的一套api设计的风格, 并不是标准。Retrofit的封装可以说是很强大,里面涉及到一堆的设计模式,可以通过注解直接配置请求,可以使用不同的http客户端,虽然默认是用http ,可以使用不同Json Converter 来序列化数据,同时提供对RxJava的支持,使用Retrofit + OkHttp + RxJava + Dagger2 可以说是目前比较潮的一套框架,但是需要有比较高的门槛。 使用教程: 主要有以下步骤: 1、get请求 2、post表单请求, 3、post 在body中发送json 3.1 retrofit 自动把java.class转成json字符串,并放在body中 3.2 重写Factory方法 手动把json字符串,并放在body中发送 3.3 通过RequestBody 手动把json字符串,并放在body中发送

    public interface PostBodyInterface { @POST("bussIntfAction!getVersionUpdateInfo.action") Call<ResponseBody> getCall(@Body PostBodyReq postBodyReq); } /** * post 在body中发送json * retrofit 自动把java.class转成json字符串,并放在body中 */ public void postBodyRequest() { HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); //设置日志Level if (BuildConfig.DEBUG) { // development build logging.setLevel(HttpLoggingInterceptor.Level.BODY); } else { // production build logging.setLevel(HttpLoggingInterceptor.Level.BASIC); } OkHttpClient.Builder httpClient = new OkHttpClient.Builder() .connectTimeout(15, TimeUnit.SECONDS) .readTimeout(15, TimeUnit.SECONDS) .writeTimeout(15, TimeUnit.SECONDS) //添加拦截器到OkHttp,这是最关键的 .addInterceptor(logging); Log.w("tan","postBodyRequest"); //步骤4:创建Retrofit对象 Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://14.116.xxx.xxx:9001/dp-bussintf/") // 设置 网络请求 Url .addConverterFactory(GsonConverterFactory.create()) //设置使用Gson解析(记得加入依赖) .client(httpClient.build()) .build(); // 步骤5:创建 网络请求接口 的实例 PostBodyInterface request = retrofit.create(PostBodyInterface.class); PostBodyReq postBodyReq=new PostBodyReq(); postBodyReq.appId=""; postBodyReq.method="bussIntfAction!getVersionUpdateInfo.action"; postBodyReq.tokenId=""; PostBodyData postBodyData=new PostBodyData(); postBodyData.versionTypeName="45"; postBodyData.versionType="1"; postBodyReq.data=postBodyData; //对 发送请求 进行封装(设置需要翻译的内容) Call<ResponseBody> call = request.getCall(postBodyReq); //步骤6:发送网络请求(异步) call.enqueue(new Callback<ResponseBody>() { //请求成功时回调 @Override public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { // 步骤7:处理返回的数据结果:输出翻译的内容 try{ Log.w("tan",response.body().string()); //不要用下面这个 //Log.w("tan",response.body().toString()); }catch (Exception e){ e.printStackTrace(); } } //请求失败时回调 @Override public void onFailure(Call<ResponseBody> call, Throwable throwable) { Log.w("tan","请求失败"); Log.w("tan",throwable.getMessage()); } }); } public interface PostBodyStringInterface { @POST("bussIntfAction!getVersionUpdateInfo.action") Call<ResponseBody> getCall(@Body RequestBody data); @Headers({"Content-Type: application/json","Accept: application/json"}) @POST("bussIntfAction!getVersionUpdateInfo.action") Call<ResponseBody> getCallForString(@Body String data); } /** * post 在body中发送json * 通过RequestBody 手动把json字符串,并放在body中发送 */ public void postBodyStringRequest() { HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); //设置日志Level if (BuildConfig.DEBUG) { // development build logging.setLevel(HttpLoggingInterceptor.Level.BODY); } else { // production build logging.setLevel(HttpLoggingInterceptor.Level.BASIC); } OkHttpClient.Builder httpClient = new OkHttpClient.Builder() .connectTimeout(15, TimeUnit.SECONDS) .readTimeout(15, TimeUnit.SECONDS) .writeTimeout(15, TimeUnit.SECONDS) //添加拦截器到OkHttp,这是最关键的 .addInterceptor(logging); Log.w("tan","postBodyRequest"); Gson gson=new Gson(); //步骤4:创建Retrofit对象 Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://14.116.xxx.xxx:9001/dp-bussintf/") // 设置 网络请求 Url .addConverterFactory(GsonConverterFactory.create(gson)) //设置使用Gson解析(记得加入依赖) .client(httpClient.build()) .build(); // 步骤5:创建 网络请求接口 的实例 PostBodyStringInterface request = retrofit.create(PostBodyStringInterface.class); String data="{\"appId\":\"\",\"data\":{\"versionType\":\"1\",\"versionTypeName\":\"45\"},\"method\":\"bussIntfAction!getVersionUpdateInfo.action\",\"tokenId\":\"\"}"; RequestBody body=RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),data); //对 发送请求 进行封装(设置需要翻译的内容) Call<ResponseBody> call = request.getCall(body); //步骤6:发送网络请求(异步) call.enqueue(new Callback<ResponseBody>() { //请求成功时回调 @Override public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { // 步骤7:处理返回的数据结果:输出翻译的内容 try{ Log.w("tan",response.body().string()); //不要用下面这个 //Log.w("tan",response.body().toString()); }catch (Exception e){ e.printStackTrace(); } } //请求失败时回调 @Override public void onFailure(Call<ResponseBody> call, Throwable throwable) { Log.w("tan","请求失败"); Log.w("tan",throwable.getMessage()); } }); } /** * post 在body中发送json * 重写Factory方法 手动把json字符串,并放在body中发送 */ public void postBodyStringForFactory() { HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); //设置日志Level if (BuildConfig.DEBUG) { // development build logging.setLevel(HttpLoggingInterceptor.Level.BODY); } else { // production build logging.setLevel(HttpLoggingInterceptor.Level.BASIC); } OkHttpClient.Builder httpClient = new OkHttpClient.Builder() .connectTimeout(15, TimeUnit.SECONDS) .readTimeout(15, TimeUnit.SECONDS) .writeTimeout(15, TimeUnit.SECONDS) //添加拦截器到OkHttp,这是最关键的 .addInterceptor(logging); Log.w("tan","postBodyRequest"); //步骤4:创建Retrofit对象 Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://14.116.xxx.xxx:9001/dp-bussintf/") // 设置 网络请求 Url .addConverterFactory(new ToStringConverterFactory()) //设置使用Gson解析(记得加入依赖) .client(httpClient.build()) .build(); // 步骤5:创建 网络请求接口 的实例 PostBodyStringInterface request = retrofit.create(PostBodyStringInterface.class); String data="{\"appId\":\"\",\"data\":{\"versionType\":\"1\",\"versionTypeName\":\"45\"},\"method\":\"bussIntfAction!getVersionUpdateInfo.action\",\"tokenId\":\"\"}"; //RequestBody body=RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),data); //对 发送请求 进行封装(设置需要翻译的内容) Call<ResponseBody> call = request.getCallForString(data); //步骤6:发送网络请求(异步) call.enqueue(new Callback<ResponseBody>() { //请求成功时回调 @Override public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { // 步骤7:处理返回的数据结果:输出翻译的内容 try{ Log.w("tan",response.body().string()); //不要用下面这个 //Log.w("tan",response.body().toString()); }catch (Exception e){ e.printStackTrace(); } } //请求失败时回调 @Override public void onFailure(Call<ResponseBody> call, Throwable throwable) { Log.w("tan","请求失败"); Log.w("tan",throwable.getMessage()); } }); } /** * get 请求 */ public void getRequest() { Log.w("tan","getRequest"); //步骤4:创建Retrofit对象 Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://fy.iciba.com/") // 设置 网络请求 Url .addConverterFactory(GsonConverterFactory.create()) //设置使用Gson解析(记得加入依赖) .build(); // 步骤5:创建 网络请求接口 的实例 GetRequest_Interface request = retrofit.create(GetRequest_Interface.class); //a=fy&f=auto&t=auto&w=hello world //对 发送请求 进行封装 Call<Translation> call = request.getCall("fy","auto","auto","hello world"); //步骤6:发送网络请求(异步) call.enqueue(new Callback<Translation>() { //请求成功时回调 @Override public void onResponse(Call<Translation> call, Response<Translation> response) { // 步骤7:处理返回的数据结果 response.body().show(); Translation result = response.body(); } //请求失败时回调 @Override public void onFailure(Call<Translation> call, Throwable throwable) { System.out.println("连接失败"); } }); } /** * post 发送表单 */ public void postFormRequest() { //步骤4:创建Retrofit对象 Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://fanyi.youdao.com/") // 设置 网络请求 Url .addConverterFactory(GsonConverterFactory.create()) //设置使用Gson解析(记得加入依赖) .build(); // 步骤5:创建 网络请求接口 的实例 PostRequest_Interface request = retrofit.create(PostRequest_Interface.class); //对 发送请求 进行封装(设置需要翻译的内容) Call<Translation1> call = request.getCall("I love you"); //步骤6:发送网络请求(异步) call.enqueue(new Callback<Translation1>() { //请求成功时回调 @Override public void onResponse(Call<Translation1> call, Response<Translation1> response) { // 步骤7:处理返回的数据结果:输出翻译的内容 Log.w("tan",response.body().getTranslateResult().get(0).get(0).toString()); } //请求失败时回调 @Override public void onFailure(Call<Translation1> call, Throwable throwable) { Log.w("tan","请求失败"); Log.w("tan",throwable.getMessage()); } }); }

    5.Volley VS OkHttp Volley的优势在于封装的更好,而使用OkHttp你需要有足够的能力再进行一次封装。而OkHttp的优势在于性能更高,因为 OkHttp基于NIO和Okio ,所以性能上要比 Volley更快。IO 和 NIO这两个都是Java中的概念,如果我从硬盘读取数据,第一种方式就是程序一直等,数据读完后才能继续操作这种是最简单的也叫阻塞式IO,还有一种是你读你的,程序接着往下执行,等数据处理完你再来通知我,然后再处理回调。而第二种就是 NIO 的方式,非阻塞式, 所以NIO当然要比IO的性能要好了,而 Okio是 Square 公司基于IO和NIO基础上做的一个更简单、高效处理数据流的一个库。理论上如果Volley和OkHttp对比的话,更倾向于使用 Volley,因为Volley内部同样支持使用OkHttp,这点OkHttp的性能优势就没了, 而且 Volley 本身封装的也更易用,扩展性更好些。

    6.OkHttp VS Retrofit 毫无疑问,Retrofit 默认是基于 OkHttp 而做的封装,这点来说没有可比性,肯定首选 Retrofit。

    7.Volley VS Retrofit 这两个库都做了不错的封装,但Retrofit解耦的更彻底,尤其Retrofit2.0出来,Jake对之前1.0设计不合理的地方做了大量重构, 职责更细分,而且Retrofit默认使用OkHttp,性能上也要比Volley占优势,再有如果你的项目如果采用了RxJava ,那更该使用 Retrofit 。所以这两个库相比,Retrofit更有优势,在能掌握两个框架的前提下该优先使用 Retrofit。但是Retrofit门槛要比Volley稍高些, 要理解他的原理,各种用法,想彻底搞明白还是需要花些功夫的,如果你对它一知半解,那还是建议在商业项目使用Volley吧。

    参考文章: https://blog.csdn.net/tantion/article/details/81112039 https://blog.csdn.net/u013068887/article/details/79542361

    作者:陈登良 原文链接:https://blog.csdn.net/qq_35448522/article/details/90437532


    最新回复(0)