Implementing a Custom Request Using Volley Library - PowerPoint PPT Presentation

About This Presentation
Title:

Implementing a Custom Request Using Volley Library

Description:

Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster. Volley is available on GitHub. – PowerPoint PPT presentation

Number of Views:9
Slides: 10
Provided by: NetleafSoftware
Category: Other

less

Transcript and Presenter's Notes

Title: Implementing a Custom Request Using Volley Library


1
Implementing a Custom Request Using Volley
Library
2
(No Transcript)
3
Why we use Volley Library? 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
connections. Transparent disk and memory response
caching with standard HTTP cache
coherence. Support for request prioritization. Can
cellation request API. You can cancel a single
request, or you can set blocks or scopes of
requests to cancel. Ease of customization, for
example, for retry and back off. Strong ordering
that makes it easy to correctly populate
your UI with data fetched asynchronously from the
network. Debugging and tracing tools Things You
Need to Do Extend the RequestltTgtclass,
where ltTgt represents the type of parsed response
the request expects. So if your parsed response
is a string, for example, create your custom
request by extending RequestltStringgt. Add Gson
library compile dependency to your app-level
build.gradle Create a model class as per
response. Add custom request to request queue of
volley
4
Create the Model Response class public class
ServiceResponse String data public String
getData() return data public void
setData(String data) this.data datapublic
int getType() return typepublic void
setType(int type) this.type typeint
type Create CustomRequest  class  public
class VolleyCustomRequest extends
RequestltObjectgtMapltString, Stringgt
paramsprotected int reequesttypeString 
postdataint postdatv1
5
protected Response.Listener mListenerpublic
VolleyCustomRequest(String url,
Response.ErrorListener listener) super(url,
listener)public VolleyCustomRequest(int
method, String url, Response.Listener listener1,
_at_Nullable Response.ErrorListener listener, String
postdata, int reequesttype) super(Method.POST,
url, listener)this.reequesttypereequesttypeth
is.mListenerlistener1this.postdatapostdatath
is.postdatv2 public VolleyCustomRequest(int
method, String url, Response.Listener listener1,
_at_Nullable Response.ErrorListener listener, int
reequesttype) super(Method.GET, url,
listener)this.reequesttypereequesttypethis.mL
istenerlistener1 public VolleyCustomRequest(in
t m, String url, Response.Listener listener1,
Response.ErrorListener listener, MapltString,
Stringgt params, int requestType)
super(Method.POST, url,listener)this.reequestt
yperequestTypethis.mListenerlistener1this.pa
ramsparams
6
_at_Overrideprotected ResponseltObjectgt
parseNetworkResponse(NetworkResponse response)
String jsonDatanew String(response.data)Servi
ceResponce snew ServiceResponce()s.setData(json
Data)s.setType(reequesttype)ResponseltObjectgt
resp Response.success((Object) (s),
HttpHeaderParser.parseCacheHeaders(response))ret
urn resp _at_Overrideprotected VolleyError
parseNetworkError(VolleyError volleyError)
ServiceResponse snew ServiceResponse()s.setDa
ta(volleyError.getLocalizedMessage())s.setType(r
eequesttype)return super.parseNetworkError(volle
yError) _at_Overrideprotected void
deliverResponse(Object response)
mListener.onResponse(response) _at_Overridepubl
ic MapltString, Stringgt getHeaders() throws
AuthFailureError MapltString,Stringgt params
new HashMapltgt()
7
params.put(Content-Type,application/x-www-form-
urlencoded)return params_at_Overrideprotected
MapltString, Stringgt getParams() throws
AuthFailureError return params _at_Override pub
lic byte getBody() throws AuthFailureError
return postdata.getBytes() Lets use
volley custom request in your app public class
YourActivity  extends AppCompatActivity
implements Response.Listener,Response.ErrorListene
r EditText et_emailTextView
back,submitLoadingDialog loadingDialog _at_Overrid
eprotected void onCreate(Bundle
savedInstanceState) super.onCreate(savedInstance
State)setContentView(R.layout.forgotpassword_lay
out)init()
8
public  void init()HashMapltString, Stringgt amp
new HashMapltgt()amp.put(user_id,et_email.getT
ext().toString())loadingDialog new
LoadingDialog(ForgotPasswordActivity.this)loadin
gDialog.showDialog()VolleyCustomRequest request
new VolleyCustomRequest(Request.Method.POST,
your url, this, this, amp, 3)RequestQueue
queue Volley.newRequestQueue(ForgotPasswordActiv
ity.this)queue.add(request) _at_Overridepublic
void onErrorResponse(VolleyError error)
loadingDialog.stop()Snackbar.with(this,null).
type(Type.ERROR).message(Some Problem
Occure).duration(Duration.SHORT).fillParent(tru
e).textAlign(Align.LEFT).show()
9
_at_Overridepublic void onResponse(Object
response) loadingDialog.stop()ServiceResponce
serviceResponce(ServiceResponce)response if(serv
iceResponce.getType()3) try // here you get
service responseJSONObject jsonObject new
JSONObject(serviceResponce.getData())String
messagejsonObject.getString(message)Toast.mak
eText(getApplicationContext(),message,Toast.LENGTH
_LONG).show()Snackbar.with(this,null).type(Type
.SUCCESS).message(message).duration(Duration.SHO
RT).fillParent(true).textAlign(Align.LEFT).show
()catch (Exception e)
Write a Comment
User Comments (0)
About PowerShow.com