Android Network Request library Volley package, make the request more convenient

Source: Internet
Author: User

First encapsulate the volley request

public class Customrequest extends Stringrequest {

private static final String TAG = CustomRequest.class.getSimpleName ();
Private String mbody;
Private map<string, string> mheaders = new hashmap<> ();

Public customrequest (int method, String URL, response.listener<string> Listener, Response.errorlistener Errorlistener) {
Super (method, URL, listener, errorlistener);
Set the message header
Mheaders.put ("Write your own content on demand", "write your own content on demand");
Mheaders.put ("Accetp", "Application/json");
Can have a lot of

}

@Override
Public map<string, String> getheaders () throws Authfailureerror {
LOG.I (TAG, mheaders.tostring ());
return mheaders;
}

@Override
Public byte[] GetBody () throws Authfailureerror {
try {
Return Textutils.isempty (mbody)? Super.getbody (): Mbody.getbytes ("Utf-8");
} catch (Unsupportedencodingexception e) {
log.e (TAG, "GetBody", e);
}
return Super.getbody ();
}

public void Setbody (String body) {
Mbody = body;
}

Public map<string, String> getresonseheaders () {
return Super.getresonseheaders ();
}

}

Next we also encapsulate a request class

public class Customrequester<resp extends Response> {
private static final String TAG = "Customrequester";
Private final Context Mcontext;
Private final class<resp> Mclazz;
Private final Com.android.volley.Response.ErrorListener Merrorlistener = new Com.android.volley.Response.ErrorListener () {
@Override
public void Onerrorresponse (Volleyerror volleyerror) {
int StatusCode = Volleyerror = = null? 0:volleyerror.networkresponse = = null? 0:volleyerror.networkresponse.statuscode;
LOG.E (TAG, "StatusCode:" + StatusCode, volleyerror);
if (Mtag = = null) {
Return
}
OnError ();
if (StatusCode = = 0) {
Onneterror ();
} else if (StatusCode = = 401) {//different request codes do different processing
} else if (StatusCode = = 404) {
} else if (StatusCode = = 400) {
} else if (StatusCode = = 403) {

} else {
String JSON = VolleyError.networkResponse.data = = null? "": New String (VolleyError.networkResponse.data);
if (! Textutils.isempty (JSON)) {
if (Mcontext instanceof Activity) {
LOG.I (TAG, "JSON" + JSON);
try {
JSON = Json.parseobject (JSON, Response.class). GetMessage ();
OnError (JSON);
} catch (Exception e) {
OnError (JSON + "not json format!");
}
}
}
}
}
};

Private final com.android.volley.response.listener<string> Mresponselistener = new Com.android.volley.response.listener<string> () {
@Override
public void Onresponse (String json) {
LOG.I (TAG, "JSON" + JSON);
CustomRequester.this.onResponse (JSON);
if (Mtag = = null) {
Return
}
try {
Final Resp Resp = Json.parseobject (JSON, Mclazz);
if (resp! = null) {
Onsuccess (RESP); The request is successful. Returns the parsed model directly here
}
} catch (Exception e) {
LOG.E (TAG, "Onresponse", e);
}
}
};
Private String Mtag;

public void Onresponse (String json) {
}


public void onsuccess (Resp Resp) {
}

public void OnError () {
}

public void Onnofounderror () {
if (Mcontext instanceof Activity) {
}
}

Public Customrequester (class<resp> clazz) {
Mcontext = null;
Mclazz = Clazz;
}

Public Customrequester (context context, class<resp> Clazz) {
Mcontext = context;
Mclazz = Clazz;
}

public void Cancelall () {
Final String tag = Mtag;
Mtag = null;
if (! Textutils.isempty (tag)) {
Myapplication.getinstance (). Getrequestqueue (). Cancelall (tag);
}
}

Post Request
Public customrequest post (string url, int page, string body) {
Final Requestqueue queue = Myapplication.getinstance (). Getrequestqueue ();
Final Customrequest request = new Customrequest (Request.Method.POST, URL, Mresponselistener, merrorlistener);
try {
Final Map<string, string> header = Request.getheaders ();
Header.remove ("Range");
if (Page > 0) {
Header.put ("Range", String.Format ("page:%1$d,max:10", page));
}
} catch (Authfailureerror authfailureerror) {
Authfailureerror.printstacktrace ();
}
Mtag = "post," + URL + "," + body;
Request.settag (Mtag);

LOG.I (TAG, Mtag);
Request.setbody (body);
Queue.add (Request);
return request;
}
Get Request
Public customrequest get (string url, int page, string body) {
Final Requestqueue queue = Yzapplication.getinstance (). Getrequestqueue ();
Final Customrequest request = new Customrequest (Request.Method.GET, URL, Mresponselistener, merrorlistener);
try {
Final Map<string, string> header = Request.getheaders ();
Header.remove ("Range");
if (Page > 0) {
Header.put ("Range", String.Format ("page:%1$d,max:10", page));
}
} catch (Authfailureerror authfailureerror) {
Authfailureerror.printstacktrace ();
}
Mtag = "post," + URL + "," + body;
Request.settag (Mtag);

LOG.I (TAG, Mtag);
Request.setbody (body);
Queue.add (Request);
return request;
}
 
Put Request
Public customrequest put (string url, int page, string body) {
Final Requestqueue queue = Myapplication.getinstance (). Getrequestqueue ();
Final Customrequest request = new Customrequest (Request.Method.PUT, URL, Mresponselistener, merrorlistener);

try {
Final Map<string, string> header = Request.getheaders ();
Header.remove ("Range");
if (Page > 0) {
Header.put ("Range", String.Format ("page:%1$d,max:10", page));
}
} catch (Authfailureerror authfailureerror) {
Authfailureerror.printstacktrace ();
}
Mtag = "put," + URL + "," + body;
Request.settag (Mtag);
Request.setbody (body);
Queue.add (Request);
return request;
}
Delete Request
Public customrequest Delete (string url, int page, string body) {
Final Requestqueue queue = Myapplication.getinstance (). Getrequestqueue ();
Final Customrequest request = new Customrequest (Request.Method.DELETE, URL, Mresponselistener, merrorlistener);
try {
Final Map<string, string> header = Request.getheaders ();
Header.remove ("Range");
if (Page > 0) {
Header.put ("Range", String.Format ("page:%1$d,max:10", page));
}
} catch (Authfailureerror authfailureerror) {
Authfailureerror.printstacktrace ();
}
Mtag = "Delete," + URL + "," + body;
Request.settag (Mtag);
Request.setbody (body);
Queue.add (Request);
return request;
}

}
Call
Response is your model.
1.
Private apprequester<response> Mrequester;

2.
Mrequester = new Apprequester<response> (this, response.class) {
    @Override
public void onsuccess (Response Response) {
Super.onsuccess (response);
Successful operation, display page, etc.
}
  
@Override
public void OnError () {
Super.onerror ();
}
};

3.
Get mode
Mrequester. Get ("URL");
Post mode  
Mrequester.post (URL, json.tojsonstring (model));
This is good, very convenient to use, you try.

Android Network Request library Volley package, make the request more convenient

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.