# volley **Repository Path**: HarmonyOS-tpc/volley ## Basic Information - **Project Name**: volley - **Description**: No description available - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 3 - **Forks**: 0 - **Created**: 2021-06-26 - **Last Updated**: 2023-04-17 ## Categories & Tags **Categories**: Uncategorized **Tags**: HarmonyOS组件 ## README # 使用方法: 1. 将volley_library.har放到相应module的libs目录下 2. 如果已经有implementation fileTree(dir: 'libs', include: ['*.jar']),则修改为implementation fileTree(dir: 'libs', include: ['*.jar','*.har']) 如果没有,直接添加依赖 implementation fileTree(dir: 'libs', include: ['*.jar','*.har']) 即可 3. 添加依赖 ``` allprojects{ repositories{ mavenCentral() } } implementation 'io.openharmony.tpc.thirdlib:volley-library:1.0.1' ``` ## 一、普通请求 ``` Text text = (Text) findComponentById(ResourceTable.Id_text); // Instantiate the RequestQueue. RequestQueue queue = Volley.newRequestQueue(this); String url ="https://httpbin.org/get"; // Request a string response from the provided URL. StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener() { @Override public void onResponse(String response) { // Display the first 500 characters of the response string. text.setText("Response is: "+ response.substring(0,500)); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { text.setText("That didn't work!"); } }); // Add the request to the RequestQueue. queue.add(stringRequest); ``` ## 取消请求 * 定义标记并将其添加到请求中 ` public static final String TAG = "MyTag"; StringRequest stringRequest; // Assume this exists. RequestQueue requestQueue; // Assume this exists. // Set the tag on the request. stringRequest.setTag(TAG); // Add the request to the RequestQueue. requestQueue.add(stringRequest);` * **在 Ability 的 onStop() 方法中,取消所有具有此标记的请求** ``` @Override protected void onStop () { super.onStop(); if (requestQueue != null) { requestQueue.cancelAll(TAG); } } ``` # 二、请求 JSON Volley 为 JSON 请求提供了以下类: JsonArrayRequest - 用于在给定网址检索 ZSONArray 响应正文的请求。 JsonObjectRequest - 用于在给定网址检索 ZSONObject 响应正文的请求,允许传入可选的 ZSONObject 作为请求正文的一部分。 这两个类均基于通用基类 JsonRequest。您可以遵循针对其他请求类型使用的同一基本模式来使用这两个类。例如,以下代码段提取了 JSON Feed 并在界面中将其显示为文本: ``` String url = "http://my-json-feed"; JsonObjectRequest jsonObjectRequest = new JsonObjectRequest (Request.Method.GET, url, null, new Response.Listener() { @Override public void onResponse(ZSONObject response) { text.setText("Response: " + response.toString()); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { // TODO: Handle error } }); // Access the RequestQueue through your singleton class. MySingleton.getInstance(this).addToRequestQueue(jsonObjectRequest); ``` # 三、实现自定义请求 大多数请求的工具箱中都有可供使用的实现;如果您的响应是字符串、图片或 JSON,那么您可能不需要实现自定义 Request。 如果您确实需要实现自定义请求,则只需执行以下操作即可: 扩展 Request 类,其中 表示请求期望的已解析响应的类型。因此,例如,如果已解析的响应是字符串,请通过扩展 Request 创建自定义请求。如需查看扩展 Request 的示例,请参阅 Volley 工具箱类 StringRequest 和 ImageRequest。 实现抽象方法 parseNetworkResponse() 和 deliverResponse(),详细说明如下所示。 parseNetworkResponse Response 封装给定类型(例如字符串、图片或 JSON)的用于传送的已解析响应。下面是 parseNetworkResponse() 实现的示例: ``` @Override protected Response parseNetworkResponse( NetworkResponse response) { try { String json = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); return Response.success(gson.fromJson(json, clazz), HttpHeaderParser.parseCacheHeaders(response)); } // handle errors // ... } ``` [查看相关文档](https://developer.android.com/training/volley/index.html "查看相关文档")