Android detailed nohttp most basic use (no encapsulation) _android

Source: Internet
Author: User
Tags response code

Nohttp is a dedicated framework for Android network requests and downloads, Nohttp basic usage is as follows

This article demo source download address: http://xiazai.jb51.net/201609/yuanma/AndroidNoHttp (jb51.net). rar

The example in this article is from the originalactivity in the demo above.

Code

For beginners, it's a little bit harder to look at other people's packaged code, especially a framework, so let's look at the most primitive uses of nohttp:

The public class Originalactivity extends Baseactivity implements View.onclicklistener {/** * is used to flag the requested what, similar to handler WHA

 T, here is used to differentiate the request * * private static final int nohttp_what_test = 0x001;

 /** * Request time waiting box/private Waitdialog mwaitdialog;

 /** * Request Queue * * Private requestqueue requestqueue; @Override protected void Onactivitycreate (Bundle savedinstancestate) {settitle ().
  Nohttptitlelist[0]);
  Setcontentview (r.layout.activity_original);

  Click on the button to listen Findview (R.id.btn_start). Setonclicklistener (this);

  Mwaitdialog = new Waitdialog (this);
  Create request queue, default concurrent 3 requests, incoming number change concurrent quantity: Nohttp.newrequestqueue (1);
 Requestqueue = Nohttp.newrequestqueue ();  @Override public void OnClick (View v) {//Create request object Request<string> requests = Nohttp.createstringrequest (URL,

  Requestmethod.post);
  Add request parameter Request.add ("UserName", "Yolanda");
  Request.add ("Userpass", 1);

  Request.add ("Userage", 1.25); Upload file Request.add ("Userhead", New FilebinaRy (new File (path));

  Add Request Header Request.addheader ("Author", "nohttp_sample"); A tag is set to return intact at the end of the request (failure/success);

  In most cases you do not need//Request.settag (object); * * What: When multiple requests use the same onresponselistener at the same time to differentiate the request, similar to handler what * Request: Requests Object * Onresponselistener callback object, accept request
 Results * * REQUESTQUEUE.ADD (nohttp_what_test, request, Onresponselistener); /** * Callback object, accept request result/private onresponselistener<string> Onresponselistener = new Onresponselistener<stri
   Ng> () {@SuppressWarnings ("unused") @Override public void onsucceed (int what, response<string> Response) {
    if (what = = Nohttp_what_test) {//Determine if what is the request//request succeeded just specified String result = Response.get ()//Response results//Response header
    Headers Headers = Response.getheaders (); Headers.getresponsecode ()//Response code Response.getnetworkmillis ()//Request time spent}} @Override public void OnStart (in
  T what) {//Request start, Display dialog mwaitdialog.show (); @Override public void onfinish (int what) {//end of request, Close dialog Mwaitdialog.dismiss (); @Override public void onfailed (int what, String URL, Object tag, charsequence error, int rescode, long ms) {//
 Request failed ...}

 };
  @Override protected void OnDestroy () {Super.ondestroy ();

 Requestqueue.cancelall ()//Stop all requests requestqueue.stop () when exiting app//stop queue when exiting app}}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.