개발자료/Android
volley
이것저것Root
2022. 5. 3. 15:19
반응형
최신버전 확인
https://google.github.io/volley/
Volley overview
Volley overview Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster. Volley is available on GitHub. Volley offers the following benefits: Automatic scheduling of network requests. Multiple concurrent network
google.github.io
build.gradle 항목추가
dependencies {
...
implementation 'com.android.volley:volley:1.2.1'
}
Simple Request
final TextView textView = (TextView) findViewById(R.id.text);
// ...
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url = "https://www.google.com";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
textView.setText("Response is: " + response.substring(0,500));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
textView.setText("That didn't work!");
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
타임아웃 설정
JsonObjectRequest jsonRequest = new JsonObjectRequest(Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// Response
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Error
}
}
);
jsonRequest.setRetryPolicy(new DefaultRetryPolicy(
60*1000, // 60Sec
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)
);
반응형