Skip to content

Instantly share code, notes, and snippets.

@LondonAppDev
Created February 23, 2017 10:26
Show Gist options
  • Save LondonAppDev/6a02d83b04fbcd240e09c834729d57db to your computer and use it in GitHub Desktop.
Save LondonAppDev/6a02d83b04fbcd240e09c834729d57db to your computer and use it in GitHub Desktop.
1602 HTTP Get Repo Function
private void getRepoList(String username) {
// First, we insert the username into the repo url.
// The repo url is defined in GitHubs API docs (https://developer.github.com/v3/repos/).
this.url = this.baseUrl + username + "/repos";
// Next, we create a new JsonArrayRequest. This will use Volley to make a HTTP request
// that expects a JSON Array Response.
// To fully understand this, I'd recommend readng the office docs: https://developer.android.com/training/volley/index.html
JsonArrayRequest arrReq = new JsonArrayRequest(Request.Method.GET, url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
// Check the length of our response (to see if the user has any repos)
if (response.length() > 0) {
// The user does have repos, so let's loop through them all.
for (int i = 0; i < response.length(); i++) {
try {
// For each repo, add a new line to our repo list.
JSONObject jsonObj = response.getJSONObject(i);
String repoName = jsonObj.get("name").toString();
String lastUpdated = jsonObj.get("updated_at").toString();
addToRepoList(repoName, lastUpdated);
} catch (JSONException e) {
// If there is an error then output this to the logs.
Log.e("Volley", "Invalid JSON Object.");
}
}
} else {
// The user didn't have any repos.
setRepoListText("No repos found.");
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// If there a HTTP error then add a note to our repo list.
setRepoListText("Error while calling REST API");
Log.e("Volley", error.toString());
}
}
);
// Add the request we just defined to our request queue.
// The request queue will automatically handle the request as soon as it can.
requestQueue.add(arrReq);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment