XHttpPost 請求與鏈接超時回調

package callbak;git

/**函數

 * this

 * @author  xulinurl

 * @see  http://my.oschina.net/u/2358780spa

 */.net

public interface XCallBack {  code

    /** rem

     * 執行回調方法 get

     * @param  objects   將處理後的結果做爲參數返回給回調方法 回調函數

     */  

    public String executeCallBack(String s);

}  

**************************************************************

package callbak;


/**

 * 

 * @author  xulin

 * @see  http://my.oschina.net/u/2358780

 */

public class XExecuteRequest implements Runnable{  

    private XExecuteResponse remote;  

    private String url;

    private String param;

    private XCallBack callBack;

    private String requestType;

    public XExecuteRequest(XExecuteResponse remote, String url, String param,XCallBack callBack) {

super();

this.remote = remote;

this.url = url;

this.param = param;

this.callBack=callBack;

}



/** 

     * 發送消息 

     */  

    public void start(String msg)  

    {  

    requestType=msg;

        Thread thread = new Thread(this);  

        thread.start();  

        System.out.println("XExecuteRequest.start>>>");  

    }  

  

    public void run() {  

    if("POST".equals(requestType)){

    remote.executePost(url, param,callBack);

    }else if("GET".equals(requestType)){

    remote.executeGet(url, param,callBack);

    }else{

    remote.executeError(callBack);

    }

    }

}  

**********************************************************

package callbak;


import com.lf.request.XHttpRequest;

/**

 * 

 * @author xulin

 * @see http://my.oschina.net/u/2358780

 */

public class XExecuteResponse {


/**

* 處理消息

* @param msg

*            接收的消息

* @param callBack

*            回調函數處理類

*/

public void executePost(String url, String param, XCallBack callBack) {

String result = XHttpRequest.sendPost(url, param);

/** 執行回調 **/

callBack.executeCallBack(result); 

System.out.println("POST:" + Thread.currentThread().getName());

System.out.println(".... run stop .....");

}


public void executeGet(String url, String param, XCallBack callBack) {

String result = XHttpRequest.sendGet(url, param);

callBack.executeCallBack(result); 

System.out.println("GET:" + Thread.currentThread().getName());

System.out.println(".... run stop .....");

}


public void executeError(XCallBack callBack) {

callBack.executeCallBack(XResultModel.errorRequest().toJsonStr());

System.out.println("ERROR:" + Thread.currentThread().getName());

System.out.println(".... run stop .....");

}


}

****************************************************

package callbak;

/**

 * 

 * @author xulin

 * @see http://my.oschina.net/u/2358780

 */

public enum XResult {

//共用的

    SUCCESS(100, "成功"),

    //手機端

    ERROR(-555, "異常"),

    CONNECT_TIMED_OUT(-999, "客戶端鏈接服務超時"),

    REQUEST_NO_TYPE(-998, "客戶端未指定正確的請求方式"),

    REQUEST_URLORPARAM_ERROR(-997, "客戶端請求地址或參數錯誤"),

    

    //服務端

    USERNAME_OR_PASSWORD_ERROR(-1001, "用戶名或密碼錯誤"),

    USER_NOT_FOUND(-1002, "用戶不存在"),

    USER_NOT_LOGIN(-1003, "用戶未登陸"),

    USER_NOT_ENABLED(-1004, "用戶未啓用");


    /**

     * 返回碼

     */

    private int code;


    /**

     * 返回結果描述

     */

    private String message;


    XResult(int code, String message) {

        this.code = code;

        this.message = message;

    }


    public int getCode() {

        return code;

    }


    public String getMessage() {

        return message;

    }


}

*****************************************************

package callbak;

/**

 * 

 * @author xulin

 * @see http://my.oschina.net/u/2358780

 */

public class XResultModel {


    /**

     * 返回碼

     */

    private int code;


    /**

     * 返回結果描述

     */

    private String message;


    /**

     * 返回內容

     */

    private Object content;


    public int getCode() {

        return code;

    }


    public void setCode(int code) {

        this.code = code;

    }


    public String getMessage() {

        return message;

    }


    public void setMessage(String message) {

        this.message = message;

    }


    public Object getContent() {

        return content;

    }


    public void setContent(Object content) {

        this.content = content;

    }


    public XResultModel(XResult status) {

        this.code = status.getCode();

        this.message = status.getMessage();

        this.content = "";

    }


    public XResultModel(XResult status, Object content) {

        this.code = status.getCode();

        this.message = status.getMessage();

        this.content = content;

    }


    public String toJsonStr(){

    return "{\"code\":\"" +code+"\","+

    "\"message\":\"" +message+"\","+

    "\"content\":\"" +content+"\""+

    "}";

    }

    

    

    public static XResultModel ok(Object content) {

        return new XResultModel(XResult.SUCCESS, content);

    }


    public static XResultModel ok() {

        return new XResultModel(XResult.SUCCESS);

    }


    public static XResultModel error(XResult error) {

        return new XResultModel(error);

    }

    

    public static XResultModel error(String errorContent) {

        return new XResultModel(XResult.ERROR,errorContent);

    }

    

    public static XResultModel errorRequest() {

        return new XResultModel(XResult.REQUEST_NO_TYPE);

    }

}

***********************  調用 *****************

String url = "http://192.168.3.254/pro";

String model = "/user";

String method = "/login";

String requestURL=url+model+method;

String param = "loginName=XX&user.loginPwd=" + MD5.toMD5("123456")+"&longitude=12.123456&latitude=32.654321";

System.out.println( MD5.toMD5("123456"));

// 如下固定寫法

XExecuteRequest local = new XExecuteRequest(new XExecuteResponse(), requestURL, param, (XCallBack) this);

time0 = System.currentTimeMillis();

local.start("POST");

System.out.println(".... other runing ....");

相關文章
相關標籤/搜索