Skip to content

Instantly share code, notes, and snippets.

@ankuryadav7
Last active December 28, 2017 12:52
Show Gist options
  • Save ankuryadav7/151f80c33986c66e8622ab51ba1bbb65 to your computer and use it in GitHub Desktop.
Save ankuryadav7/151f80c33986c66e8622ab51ba1bbb65 to your computer and use it in GitHub Desktop.
Call GET method with parameters via volley. YouTube search API used for demo.
String URL="https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UCN2W5ND2GDEqw4NSauNmbRw&maxResults=25&key=API KEY PASTE HERE&type=video";
StringRequest stringRequest=new StringRequest(Request.Method.GET, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject=new JSONObject(response);
JSONArray jsonArray=jsonObject.getJSONArray("items");
for(int i=0;i<jsonArray.length();i++){
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
JSONObject jsonVideoId=jsonObject1.getJSONObject("id");
String videoId=jsonVideoId.getString("videoId");
JSONObject jsonsnippet= jsonObject1.getJSONObject("snippet");
String videoTitle=jsonsnippet.getString("title");
JSONObject imageObject = jsonsnippet.getJSONObject("thumbnails").getJSONObject("high");
String imageUrl=imageObject.getString("url");
Log.w("videoFetched r",videoId+" "+videoTitle+" "+imageUrl);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
Log.w("videoFetched e ",error+"");
}
});
RequestQueue requestQueue = Volley.newRequestQueue(context);
int socketTimeout = 10000;
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
jsonRequest.setRetryPolicy(policy);
requestQueue.add(jsonRequest);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment