public class VolleySampleApplication extends Application { private RequestQueue queue; @Override public void onCreate() { this.queue = Volley.newRequestQueue(this); } public RequestQueue getRequestQueue() { return queue; } }
<application android:name=".VolleySampleApplication" android:allowBackup="true" ... </application>
StringRequest strReq = new StringRequest(Request.Method.POST, url, this, this) { @Override protected Map getParams(){ Map params = new HashMap(); params.put("text", textEditText.getText().toString()); return params; } } VolleySampleApplication app = (VolleySampleApplication) getApplication(); app.getRequestQueue().add(strReq);
@Override public void onResponse(String s) { if (s != null) { try { JSONObject jsonObject = new JSONObject(s); if (jsonObject != null) { int words = jsonObject.getInt("words"); updateWordsCount(words); } } catch (JSONException e) { e.printStackTrace(); } } } @Override public void onErrorResponse(VolleyError volleyError) { volleyError.printStackTrace(); }