你如何使用Android Volley API?

我正在考虑在我的下一个项目中实现Android Volley库( 关于Volley的Google IO演示文稿 )。

但是,我还没有发现任何严重的API库。

如何上传文件,执行POST / GET请求,并使用Volley将Gsonparsing器添加为JSONparsing器?

源代码

编辑:最后在这里是关于“排球图书馆” 的官方培训

我find了关于Volley图书馆的一些例子

  • Ognyan Bankov的6个例子 :

    • 简单的要求
    • JSON请求
    • Gson请求
    • 图片加载
    • 与更新的外部HttpClient(4.2.3)
    • 使用自签名的SSL证书。
  • 帕里什·玛雅尼(Paresh Mayani)的一个简单的例子

  • 另一个例子是HARDIK TRIVEDI

  • ) Android与 Ravi Tamada的Volley图书馆合作

希望这可以帮助你

不幸的是,直到现在,还没有像JavaDocs这样的Volley库的文档。 只有github上的回购和互联网上的几个教程。 所以唯一的好文档是源代码 :)。 当我玩Volley时,我读了这个教程 。

关于post / get你可以阅读: Volley – POST / GET参数

希望这可以帮助

这是使用Volley进行POST请求的示例。 StringRequest用于以String的forms获得响应。
假设你的rest API返回一个JSON。 您的API的JSON响应在此处以stringforms接收,您可以再次转换为JSON并进一步处理。 在代码中添加了注释。

 StringRequest postRequest = new StringRequest(Request.Method.POST, "PUT_YOUR_REST_API_URL_HERE", new Response.Listener<String>() { @Override public void onResponse(String response) { try { final JSONObject jsonObject = new JSONObject(response); // Process your json here as required } catch (JSONException e) { // Handle json exception as needed } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { String json = null; NetworkResponse response = error.networkResponse; if(response != null && response.data != null){ switch(response.statusCode) { default: String value = null; try { // It is important to put UTF-8 to receive proper data else you will get byte[] parsing error. value = new String(response.data, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } json = trimMessage(value, "message"); // Use it for displaying error message to user break; } } loginError(json); progressDialog.dismiss(); error.printStackTrace(); } public String trimMessage(String json, String key){ String trimmedString = null; try{ JSONObject obj = new JSONObject(json); trimmedString = obj.getString(key); } catch(JSONException e){ e.printStackTrace(); return null; } return trimmedString; } } ) { @Override protected Map<String, String> getParams() { Map<String, String> params = new HashMap<>(); params.put("abc", "pass abc"); params.put("xyz", "pass xyz"); // Pass more params as needed in your rest API // Example you may want to pass user input from EditText as a parameter // editText.getText().toString().trim() return params; } @Override public String getBodyContentType() { // This is where you specify the content type return "application/x-www-form-urlencoded; charset=UTF-8"; } }; // This adds the request to the request queue MySingleton.getInstance(YourActivity.this) .addToRequestQueue(postRequest); 

//下面是MySingleton类

 public class MySingleton { private static MySingleton mInstance; private RequestQueue mRequestQueue; private static Context mCtx; private MySingleton(Context context) { mCtx = context; mRequestQueue = getRequestQueue(); } public static synchronized MySingleton getInstance(Context context) { if (mInstance == null) { mInstance = new MySingleton(context); } return mInstance; } public RequestQueue getRequestQueue() { if (mRequestQueue == null) { // getApplicationContext() is key, it keeps you from leaking the // Activity or BroadcastReceiver if someone passes one in. mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext()); } return mRequestQueue; } public <T> void addToRequestQueue(Request<T> req) { getRequestQueue().add(req); } } 

只需添加volley.jar库到您的项目。 接着

根据Android文档:

 // Instantiate the RequestQueue. RequestQueue queue = Volley.newRequestQueue(this); String url ="http://www.google.com"; // Request a string response from the provided URL. StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() { @Override public void onResponse(String response) { // process your response here } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { //perform operation here after getting error } }); // Add the request to the RequestQueue. queue.add(stringRequest); 

如需更多帮助,请参阅如何使用“排球”

使用这个类,它可以为您提供连接数据库的简单方法!

 public class WebRequest { private Context mContext; private String mUrl; private int mMethod; private VolleyListener mVolleyListener; public WebRequest(Context context) { mContext = context; } public WebRequest setURL(String url) { mUrl = url; return this; } public WebRequest setMethod(int method) { mMethod = method; return this; } public WebRequest readFromURL() { RequestQueue requestQueue = Volley.newRequestQueue(mContext); StringRequest stringRequest = new StringRequest(mMethod, mUrl, new Response.Listener<String>() { @Override public void onResponse(String s) { mVolleyListener.onRecieve(s); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { mVolleyListener.onFail(volleyError); } }); requestQueue.add(stringRequest); return this; } public WebRequest onListener(VolleyListener volleyListener) { mVolleyListener = volleyListener; return this; } public interface VolleyListener { public void onRecieve(String data); public void onFail(VolleyError volleyError); } 

}

如果你想使用它,只要关注前:

  new WebRequest(mContext) .setURL("http://google.com") .setMethod(Request.Method.POST) .readFromURL() .onListener(new WebRequest.VolleyListener() { @Override public void onRecieve(String data) { } @Override public void onFail(VolleyError volleyError) { } });