有幾種方法可以執行Rest API。一個是通用異步任務,但我個人覺得它太泛化了。作爲替代方案,您可以使用Volley從Android App進行網絡請求。
- 添加
Volley
到build.gradle
文件:
dependencies {
compile 'com.android.volley:volley:1.1.0'
}
拿上你的應用程序android.permission.INTERNET
權限。
實例化排球請求隊列與應用context
:
// Instantiate the RequestQueue
RequestQueue queue = Volley.newRequestQueue(this);
- 爲
JsonArray
或JsonObject
創建請求:
String url = "https://api.instagram.com/v1/users/self/media/recent/?access_token=DUMMY-ACCESS-TOKEN";
JsonObjectRequest jsObjRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener() {
@Override
public void onResponse(JSONObject response) {
// Set up operation for network callback here
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
});
- 將請求添加到v OLLEY
RequestQueue
:
// queue object was instantiated in pt#3
queue.add(jsObjRequest);
您可以使用改造或凌空請求。對於圖像,最好使用Glide或Picasso –
這可能有助於https://github.com/ashokslsk/Instagram-Integration –
這是一個REST Api,因此只需創建自己的客戶端即可。 – canillas