dependencies {
...
implementation 'com.android.volley:volley:1.1.1'
}
public class MyApplication extends Application {
private static MyApplication instance;
private RequestQueue requestQueue;
@Override
public void onCreate() {
super.onCreate();
instance = this;
requestQueue = Volley.newRequestQueue(getApplicationContext());
}
public static synchronized MyApplication getInstance() {
return instance;
}
public RequestQueue getRequestQueue() {
return requestQueue;
}
}
String url = "http://example.com/api/data";
RequestQueue queue = MyApplication.getInstance().getRequestQueue();
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
queue.add(stringRequest);
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
String data = jsonObject.getString("data");
} catch (JSONException e) {
e.printStackTrace();
}
}
public void onErrorResponse(VolleyError error) {
}