Android Http异步请求,Callback

开发HTML5和远程交互,采用JSONP,是异步方式。Android的异步方式不太一样,采用的是多线程和Handler的方式处理。

1 首先是HttpConnection,方法包括HttPost, HttpGet

001package com.juupoo.common;
002 
003import java.util.ArrayList;
004import java.util.List;
005 
006import org.apache.http.HttpResponse;
007import org.apache.http.NameValuePair;
008import org.apache.http.client.HttpClient;
009import org.apache.http.client.entity.UrlEncodedFormEntity;
010import org.apache.http.client.methods.HttpGet;
011import org.apache.http.client.methods.HttpPost;
012import org.apache.http.impl.client.DefaultHttpClient;
013import org.apache.http.message.BasicNameValuePair;
014import org.apache.http.params.BasicHttpParams;
015import org.apache.http.params.HttpConnectionParams;
016import org.apache.http.params.HttpParams;
017import org.apache.http.util.EntityUtils;
018 
019import android.os.Bundle;
020import android.os.Handler;
021import android.os.Message;
022 
023/**
024 * Asynchronous HTTP connections
025 *
026 * @author Greg Zavitz & Joseph Roth
027 */
028public class HttpConnection implements Runnable {
029 
030    public static final int DID_START = 0;
031    public static final int DID_ERROR = 1;
032    public static final int DID_SUCCEED = 2;
033 
034    private static final int GET = 0;
035    private static final int POST = 1;
036    private static final int PUT = 2;
037    private static final int DELETE = 3;
038    private static final int BITMAP = 4;
039 
040    private String url;
041    private int method;
042    private String data;
043    private CallbackListener listener;
044 
045    private HttpClient httpClient;
046 
047    // public HttpConnection() {
048    // this(new Handler());
049    // }
050 
051    public void create(int method, String url, String data, CallbackListener listener) {
052        this.method = method;
053        this.url = url;
054        this.data = data;
055        this.listener = listener;
056        ConnectionManager.getInstance().push(this);
057    }
058 
059    public void get(String url) {
060        create(GET, url, null, listener);
061    }
062 
063    public void post(String url, String data, CallbackListener listener) {
064        create(POST, url, data, listener);
065    }
066 
067    public void put(String url, String data) {
068        create(PUT, url, data, listener);
069    }
070 
071    public void delete(String url) {
072        create(DELETE, url, null, listener);
073    }
074 
075    public void bitmap(String url) {
076        create(BITMAP, url, null, listener);
077    }
078 
079    public interface CallbackListener {
080        public void callBack(String result);
081    }
082 
083    private static final Handler handler = new Handler() {
084        @Override
085        public void handleMessage(Message message) {
086            switch (message.what) {
087                case HttpConnection.DID_START: {
088                    break;
089                }
090                case HttpConnection.DID_SUCCEED: {
091                    CallbackListener listener = (CallbackListener) message.obj;
092                    Object data = message.getData();
093                    if (listener != null) {
094                        if(data != null) {
095                            Bundle bundle = (Bundle)data;
096                            String result = bundle.getString("callbackkey");
097                            listener.callBack(result);
098                        }
099                    }
100                    break;
101                }
102                case HttpConnection.DID_ERROR: {
103                    break;
104                }
105            }
106        }
107    };
108 
109    public void run() {
110//      handler.sendMessage(Message.obtain(handler, HttpConnection.DID_START));
111        httpClient = getHttpClient();
112        try {
113            HttpResponse httpResponse = null;
114            switch (method) {
115            case GET:
116                httpResponse = httpClient.execute(new HttpGet(
117                        StaticInfos.Server_URL + url));
118                break;
119            case POST:
120                HttpPost httpPost = new HttpPost(StaticInfos.Server_URL
121                        + url);
122                List<NameValuePair> params = new ArrayList<NameValuePair>();
123                BasicNameValuePair valuesPair = new BasicNameValuePair("args",
124                        data);
125                params.add(valuesPair);
126                httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
127                httpResponse = httpClient.execute(httpPost);
128                if (isHttpSuccessExecuted(httpResponse)) {
129                    String result = EntityUtils.toString(httpResponse
130                            .getEntity());
131                    this.sendMessage(result);
132                } else {
133                    this.sendMessage("fail");
134                }
135                break;
136            }
137        } catch (Exception e) {
138            this.sendMessage("fail");
139        }
140        ConnectionManager.getInstance().didComplete(this);
141    }
142 
143    // private void processBitmapEntity(HttpEntity entity) throws IOException {
144    // BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
145    // Bitmap bm = BitmapFactory.decodeStream(bufHttpEntity.getContent());
146    // handler.sendMessage(Message.obtain(handler, DID_SUCCEED, bm));
147    // }
148 
149    private void sendMessage(String result) {
150        Message message = Message.obtain(handler, DID_SUCCEED,
151                listener);
152        Bundle data = new Bundle();
153        data.putString("callbackkey", result);
154        message.setData(data);
155        handler.sendMessage(message);
156 
157    }
158 
159    public static DefaultHttpClient getHttpClient() {
160        HttpParams httpParams = new BasicHttpParams();
161        HttpConnectionParams.setConnectionTimeout(httpParams, 20000);
162        HttpConnectionParams.setSoTimeout(httpParams, 20000);
163        // HttpConnectionParams.setSocketBufferSize(httpParams, 8192);
164 
165        DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);
166        return httpClient;
167    }
168 
169    public static boolean isHttpSuccessExecuted(HttpResponse response) {
170        int statusCode = response.getStatusLine().getStatusCode();
171        return (statusCode > 199) && (statusCode < 400);
172    }
173 
174}

 

2  ConnectionManager类,将线程添加到队列中
01package com.juupoo.common;
02 
03 
04import java.util.ArrayList;
05 
06/**
07 * Simple connection manager to throttle connections
08 *
09 * @author Greg Zavitz
10 */
11public class ConnectionManager {
12 
13    public static final int MAX_CONNECTIONS = 5;
14 
15    private ArrayList<Runnable> active = new ArrayList<Runnable>();
16    private ArrayList<Runnable> queue = new ArrayList<Runnable>();
17 
18    private static ConnectionManager instance;
19 
20    public static ConnectionManager getInstance() {
21        if (instance == null)
22            instance = new ConnectionManager();
23        return instance;
24    }
25 
26    public void push(Runnable runnable) {
27        queue.add(runnable);
28        if (active.size() < MAX_CONNECTIONS)
29            startNext();
30    }
31 
32    private void startNext() {
33        if (!queue.isEmpty()) {
34            Runnable next = queue.get(0);
35            queue.remove(0);
36            active.add(next);
37 
38            Thread thread = new Thread(next);
39            thread.start();
40        }
41    }
42 
43    public void didComplete(Runnable runnable) {
44        active.remove(runnable);
45        startNext();
46    }
47 
48}

 

3 调用:
01new HttpConnection().post("user.login", args, callbackListener);
02 
03private CallbackListener callbackListener = newHttpConnection.CallbackListener() {
04        @Override
05        public void callBack(String v) {
06            if(v != "fail") {
07                if("false".equals(v)) {
08                    LoginActivity.this.showInfo(R.string.username_or_pass_error);
09                } else {
10                    // 登录
11                    Intent intent = new Intent();
12                    intent.setClass(LoginActivity.this, MainActivity.class);
13                    LoginActivity.this.startActivity(intent);
14                }
15            } else {
16                LoginActivity.this.showInfo(R.string.network_transfer_error);
17            }
18            progressDialog.dismiss();
19        }

20    };
可参考本文。

http://masl.cis.gvsu.edu/2010/04/05/android-code-sample-asynchronous-http-connections/

原文地址:http://www.android100.org/html/201204/07/733.html


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值