compile com.mcxiaoke.volley:library:1.0.+
========================================
MainActivity.java
===================
package com.sam.web.restdemo;
import android.support.v7.app.AppCompatActivity;
import" />
compile com.mcxiaoke.volley:library:1.0.+
========================================
MainActivity.java
===================
package com.sam.web.restdemo;
import android.support.v7.app.AppCompatActivity;
import" />
<uses-permission android:name="android.permission.INTERNET" />
compile com.mcxiaoke.volley:library:1.0.+
========================================
MainActivity.java
===================
package com.sam.web.restdemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.Response;
import com.android.volley.Request;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONException;
public class MainActivity extends AppCompatActivity {
EditText etGitHubUser; // This will be a reference to our GitHub username input.
Button btnGetRepos; // This is a reference to the "Get Repos" button.
TextView tvRepoList; // This will reference our repo list text box.
RequestQueue requestQueue; // This is our requests queue to process our HTTP requests.
String baseUrl = "https://api.github.com/users/"; // This is the API base URL (GitHub API)
String url; // This will hold the full URL which will include the username entered in the etGitHubUser.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.etGitHubUser = (EditText) findViewById(R.id.et_github_user); // Link our github user text box.
this.btnGetRepos = (Button) findViewById(R.id.btn_get_repos); // Link our clicky button.
this.tvRepoList = (TextView) findViewById(R.id.tv_repo_list); // Link our repository list text output box.
this.tvRepoList.setMovementMethod(new ScrollingMovementMethod()); // This makes our text box scrollable, for those big GitHub contributors with lots of repos :)
requestQueue = Volley.newRequestQueue(this); // This setups up a new request queue which we will need to make HTTP requests.
}
private void clearRepoList() {
// This will clear the repo list (set it as a blank string).
this.tvRepoList.setText("");
}
private void addToRepoList(String repoName, String lastUpdated) {
// This will add a new repo to our list.
// It combines the repoName and lastUpdated strings together.
// And then adds them followed by a new line ( make two new lines).
String strRow = repoName + " / " + lastUpdated;
String currentText = tvRepoList.getText().toString();
this.tvRepoList.setText(currentText + " " + strRow);
}
private void setRepoListText(String str) {
// This is used for setting the text of our repo list box to a specific string.
// We will use this to write a No repos found" message if the user doenst have any.
this.tvRepoList.setText(str);
}
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, Id 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 lets 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 didnt 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);
}
public void getReposClicked(View v) {
// Clear the repo list (so we have a fresh screen to add to)
clearRepoList();
// Call our getRepoList() function that is defined above and pass in the
// text which has been entered into the etGitHubUser text input field.
getRepoList(etGitHubUser.getText().toString());
}
}
===================================================================================================
activity_main.xml
==================
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
tools:context="com.sam.web.restdemo.MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:hint="GitHub Username"
android:text="LondonAppDev"
android:ems="10"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/et_github_user"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:text="Get Repos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/et_github_user"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/btn_get_repos"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="getReposClicked" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_get_repos"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/tv_repo_list"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:scrollbars="vertical"
android:layout_alignParentEnd="true" />
</RelativeLayout>