eccolo:
STEP 1: rendere disponibile la classe Volley
pare che le future linee guida di google prevedano di incorporare la classe nel proprio progetto (cosa facilmente ottenibile tramite il Git)
io però ho preferito un approccio classico aggiungendo la dipendenza:
CODICE:
compile 'com.mcxiaoke.volley:library-aar:1.0.0'
STEP 2: aggiungere al manifesto il consueto accesso a internet:
CODICE:
<uses-permission android:name="android.permission.INTERNET" />
adesso le classi:
STEP 3: CustomJSONObjectRequest.java che si occupa di fare la richiesta del file
CODICE:
import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.RetryPolicy;
import com.android.volley.toolbox.JsonObjectRequest;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class CustomJSONObjectRequest extends JsonObjectRequest {

public CustomJSONObjectRequest(int method, String url, JSONObject jsonRequest,
Response.Listener<JSONObject> listener,
Response.ErrorListener errorListener) {
super(method, url, jsonRequest, listener, errorListener);
}

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}

@Override
public RetryPolicy getRetryPolicy() {
return super.getRetryPolicy();
}
}
STEP 4: CustomVolleyRequestQueue.java che si occupa di incalanare la coda delle richieste
CODICE:
import android.content.Context;

import com.android.volley.Cache;
import com.android.volley.Network;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.BasicNetwork;
import com.android.volley.toolbox.DiskBasedCache;
import com.android.volley.toolbox.HurlStack;

public class CustomVolleyRequestQueue {

private static CustomVolleyRequestQueue mInstance;
private static Context mCtx;
private RequestQueue mRequestQueue;

private CustomVolleyRequestQueue(Context context) {
mCtx = context;
mRequestQueue = getRequestQueue();
}

public static synchronized CustomVolleyRequestQueue getInstance(Context context) {
if (mInstance == null) {
mInstance = new CustomVolleyRequestQueue(context);
}
return mInstance;
}

public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
Cache cache = new DiskBasedCache(mCtx.getCacheDir(), 10 * 1024 * 1024);
Network network = new BasicNetwork(new HurlStack());
mRequestQueue = new RequestQueue(cache, network);
mRequestQueue.start();
}
return mRequestQueue;
}
}
STEP 5: il codice che fa la richiesta nella Main Activity
CODICE:
mQueue = CustomVolleyRequestQueue.getInstance(this.getAppli cationContext()).getRequestQueue();
String url = "urldelfiledascaricare";
final CustomJSONObjectRequest jsonRequest = new CustomJSONObjectRequest(Request.Method.GET, url, new JSONObject(), this, this);
mQueue.add (jsonRequest);
STEP 6: gestione della risposta
CODICE:
@Override
public void onResponse(Object response) {
try {
//fai quello che ti pare con l'oggetto 'response' ottenuto
} catch (JSONException e){
e.printStackTrace();
}