APP开发公司分享Volley网络库的使用,为了使用方便,可以对Volley库提供的类和方法进行二次封装,深圳APP开发公司归纳整理其代码如下所示。
public class JsonRequest extends JsonObjectRequest { String cookieString; public JsonRequest( String url, JSONObject jsonRequest, Response. ErrorListener errorListener) { super( url, jsonRequest, listener, errorListener); } //自定义向服务器发送的头字段数据 @ Override public Map< String, String> getHeaders() throws AuthFailureError { HashMap< String, String> headers = new HashMap< String, String>(); // 标示是Android APP向服务器发起请求 headers. put(" AppKey", "Android"); // 设置 User- Agent 的内容为 APP 的包名和版本信息,标示是哪个APP向服务器发起请求 String packageName = context. getPackageName(); PackageInfo info = context. getPackageManager(). getPackageInfo( packageName, 0); userAgent = packageName + "/" + info. versionCode; headers. put(" User- Agent", userAgent ); //把服务器返回 给 APP 的 Cookie 信息, 添加到 PP发给服务器的头信息中, 标示访问服务器的客户身份 headers. put(" Cookie", cookieString); return headers; } @ Override protected Response< JSONObject> parseNetworkResponse( NetworkResponse response) { try { String jsonString = new String( response. data, HttpHeaderParser. parseCharset( response. headers, PROTOCOL_ CHARSET)); // 获取服务器返回给APP的Cookie信息 Map< String, String> headers = response. headers; cookieString = headers. get(" Set- Cookie"); // 解析服务器返回的 cookie 信息,从中获取 SessionID String sessionId = parseVooleyCookie( cookies); return Response. success( new JSONObject( jsonString), HttpHeaderParser. parseCacheHeaders( response)); } catch (UnsupportedEncodingException e) { return Response. error( new ParseError( e)); } catch (JSONException je) { return Response. error( new ParseError( je)); }
} // 解析 cookie 数据 public String parseVooleyCookie( String cookie) { StringBuilder sb = new StringBuilder(); String[] cookievalues = cookie. split(";"); for (int j = 0; j < cookievalues. length; j++) { String[] keyPair = cookievalues[ j]. split("/"); for (int i = 0; i < keyPair. length; i++) { if (keyPair[ 0]. contains(" session_ id")) { sb. append( keyPair[ 0]); sb. append(";"); break; } } } return sb. toString(); } }
使用OkHttp3作为Volley库的HTTP协议栈,需要实现HttpStack里定义的接口方法。APP开发公司整理协议栈的具体代码如下:
public class OkHttp3Stack implements HttpStack {
private final OkHttpClient mClient; public OkHttp3Stack( OkHttpClient client) { this. mClient = client; } @ Override public HttpResponse performRequest( Request<?> request, Map< String, String> additionalHeaders) throws IOException, AuthFailureError { int timeoutMs = request. getTimeoutMs(); OkHttpClient client = mClient. newBuilder() . readTimeout( timeoutMs, TimeUnit. MILLISECONDS) . connectTimeout( timeoutMs, TimeUnit. MILLISECONDS) . writeTimeout( timeoutMs, TimeUnit. MILLISECONDS) . build(); okhttp3. Request. Builder okHttpRequestBuilder = new okhttp3. Request. Builder(); Map< String, String> headers = request. getHeaders(); for (final String name : headers. keySet()) { okHttpRequestBuilder. addHeader( name, headers. get( name)); } for (final String name : additionalHeaders. keySet()) {
okHttpRequestBuilder. addHeader( name, additionalHeaders. get( name)); } setConnectionParametersForRequest( okHttpRequestBuilder, request); okhttp3. Request okhttp3Request = okHttpRequestBuilder. url( request. getUrl()). build(); Response okHttpResponse = client. newCall( okhttp3Request). execute(); StatusLine responseStatus = new BasicStatusLine ( parseProtocol( okHttpResponse. protocol()), okHttpResponse. code(), okHttpResponse. message() ); BasicHttpResponse response = new BasicHttpResponse( responseStatus); response. setEntity( entityFromOkHttpResponse( okHttpResponse)); Headers responseHeaders = okHttpResponse. headers();
for (int i = 0, len = responseHeaders. size(); i < len; i++) { final String name = responseHeaders. name( i), value = responseHeaders. value( i); if (name != null) { response. addHeader( new BasicHeader( name, value)); } } return response; } private static HttpEntity entityFromOkHttpResponse( Response response) throws IOException { BasicHttpEntity entity = new BasicHttpEntity(); ResponseBody body = response. body(); entity. setContent( body. byteStream()); entity. setContentLength( body. contentLength()); entity. setContentEncoding( response. header(" Content- Encoding")); if (body. contentType() != null) { entity. setContentType( body. contentType(). type()); } return entity; }
@ SuppressWarnings(" deprecation") private static void setConnectionParametersForRequest ( okhttp3. Request. Builder builder, Request<?> request) throws IOException, AuthFailureError { switch (request. getMethod()) { case Request. Method. DEPRECATED_ GET_ OR_ POST: byte[] postBody = request. getPostBody(); if (postBody != null) { builder. post( RequestBody. create ( MediaType. parse( request. getPostBodyContentType()), postBody)); } break; case Request. Method. GET: builder. get(); break; case Request. Method. DELETE: builder. delete(); break; case Request. Method. POST: builder. post( createRequestBody( request)); break;
case Request. Method. PUT: builder. put( createRequestBody( request)); break; case Request. Method. HEAD: builder. head(); break; case Request. Method. OPTIONS: builder. method(" OPTIONS", null); break; case Request. Method. TRACE: builder. method(" TRACE", null); break; case Request. Method. PATCH: builder. patch( createRequestBody( request)); break; default: throw new IllegalStateException(" Unknown method type."); } }
private static RequestBody createRequestBody( Request request) throws AuthFailureError { final byte[] body = request. getBody(); if (body == null) return null; return RequestBody. create( MediaType. parse( request. getBodyContentType()), body); } private static ProtocolVersion parseProtocol( final Protocol protocol) { switch (protocol) { case HTTP_ 1_ 0: return new ProtocolVersion(" HTTP", 1, 0); case HTTP_ 1_ 1: return new ProtocolVersion(" HTTP", 1, 1); case SPDY_ 3: return new ProtocolVersion(" SPDY", 3, 1); case HTTP_ 2: return new ProtocolVersion(" HTTP", 2, 0); } throw new IllegalAccessError(" Unkwown protocol"); } }
还需增加一个类,提供其他模块调用Volley库的接口方法。
public class NetworkManager { private static NetworkManager mInstance; private RequestQueue mRequestQueue; private static Context mCtx; private NetworkManager( Context context) { mCtx = context; mRequestQueue = getRequestQueue(); } // 使用单列模式创建类的实例 public static synchronized NetworkManager getInstance( Context context) { if (mInstance == null) { mInstance = new NetworkManager( context); } return mInstance; } public RequestQueue getRequestQueue() { if (mRequestQueue == null) { // 设置 使用 OkHttp3 作为 HTTP 协议栈 mRequestQueue = Volley. newRequestQueue( mCtx, new
OkHttp3Stack( new OkHttpClient())); } return mRequestQueue; } private < T> Request< T> add( Request< T> request) { return mRequestQueue. add( request);// 添加请求到队列 } /** * 创建 JSON 格式的请求数据 * */ public void JsonRequest( Object tag, String url, JSONObject jsonObject, Response. Listener< JSONObject> listener, Response. ErrorListener errorListener) { JsonRequest jsonRequest; jsonRequest = new JsonRequest( url, jsonObject, listener, errorListener); jsonRequest. setTag( tag); add( jsonRequest); }
/** * 取消请求 * */ public void cancel( Object tag) { mRequestQueue. cancelAll( tag); } }
函数调用方式如下:
NetworkManager. getInstance( EamApplication. getContext()). JsonRequest( TAG, baseUrl + url, jsonObject, new Response. Listener< JSONObject>() { @ Override public void onResponse( JSONObject jsonObject) { Log. v( TAG, "response json 对象: " + jsonObject. toString()); } }, new Response. ErrorListener() { @ Override public void onErrorResponse( VolleyError error) { Log. e( TAG, error. getMessage(), error);
} });
在创建网络对象实例时,使用Application级别的Context,保证实例的生命周期与APP的生命周期一样。好了,APP开发公司本文就“开发APP对于怎样封装Volley网络库代码详解”实操知识分享就到这里,谢谢关注,博纳网络编辑整理。