Android interface callback function

Source: Internet
Author: User

Android interface callback function

Instance code:

 

(new OnHttpListener() {@Overridepublic void onStart() {// TODO Auto-generated method stubsuper.onStart();}@Overridepublic void onProgress(int mProgress, String message) {// TODO Auto-generated method stubsuper.onProgress(mProgress, message);}@Overridepublic void onSuccess(boolean status, int resultCode, String message,String data) {// TODO Auto-generated method stub}@Overridepublic void onFail(Exception e, String message) {// TODO Auto-generated method stub}@Overridepublic void onFinish() {// TODO Auto-generated method stubsuper.onFinish();}});

This type of code is often encountered during development. It looks mysterious. The onSuccess () and onFail methods are reloaded, and other methods need to be called by themselves. In fact, it is very simple and not nonsense. You can directly go to the Code:

 

 

/*** Process control start and end callback functions * @ author LanYan **/public abstract interface OnProcessListener extends BaseListener {/*** start task */void onStart (); /*** end of task */void onFinish ();}

Progress control:

 

 

/*** Progress control ** @ author LanYan **/public abstract class OnProgressListener implements OnProcessListener {/*** display control of progress bar * @ param mProgress * @ param message */public abstract void onProgress (int mProgress, string message); @ Overridepublic void onStart () {// TODO Auto-generated method stub} @ Overridepublic void onFinish () {// TODO Auto-generated method stub}

Htt request:

 

 

/*** HTTP access progress control ** @ author LanYan **/public abstract class OnHttpListener extends OnProgressListener {@ Overridepublic void onProgress (int mProgress, String message) {// TODO Auto-generated method stub}/*** callback function for request failure, returns the Exception information and Status Code * @ param e * @ param message */public abstract void onFail (Exception e, String message);/*** callback function for successful request, return response data * @ param status * @ param message * @ param data * @ param resultCode */public abstract void onSuccess (boolean status, int resultCode, String message, String data );}

Test class:

 

 

public class Test  {public static void main(String[] args) {getHttpData(HttpType.post, url, parmas, new OnHttpListener() {/*@Overridepublic void onStart() {// TODO Auto-generated method stubsuper.onStart();}*/@Overridepublic void onSuccess(boolean status, int resultCode, String message,String data) {// TODO Auto-generated method stub}@Overridepublic void onFail(Exception e, String message) {// TODO Auto-generated method stub}/*@Overridepublic void onFinish() {// TODO Auto-generated method stubsuper.onFinish();}*/});}public static void getHttpData(String type,String url,NameValuePair parmas,OnHttpListener listener){}public enum HttpType{get,post,defult}}


 

 

Related Article

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.