之前一直在用HTML5开发移动本地应用,后来发现,实际上HTML5开发的本地应用,开发效率高,而且跨平台,但是体验,相应无法和原生应用,还有一定差距。
开发HTML5和远程交互,采用JSONP,是异步方式。Android的异步方式不太一样,采用的是多线程和Handler的方式处理。
1 首先是HttpConnection,方法包括HttPost, HttpGet
001 |
package com.juupoo.common; |
003 |
import java.util.ArrayList; |
004 |
import java.util.List; |
006 |
import org.apache.http.HttpResponse; |
007 |
import org.apache.http.NameValuePair; |
008 |
import org.apache.http.client.HttpClient; |
009 |
import org.apache.http.client.entity.UrlEncodedFormEntity; |
010 |
import org.apache.http.client.methods.HttpGet; |
011 |
import org.apache.http.client.methods.HttpPost; |
012 |
import org.apache.http.impl.client.DefaultHttpClient; |
013 |
import org.apache.http.message.BasicNameValuePair; |
014 |
import org.apache.http.params.BasicHttpParams; |
015 |
import org.apache.http.params.HttpConnectionParams; |
016 |
import org.apache.http.params.HttpParams; |
017 |
import org.apache.http.util.EntityUtils; |
019 |
import android.os.Bundle; |
020 |
import android.os.Handler; |
021 |
import android.os.Message; |
024 |
* Asynchronous HTTP connections |
026 |
* @author Greg Zavitz & Joseph Roth |
028 |
public class HttpConnection implements Runnable { |
030 |
public static final int DID_START = 0; |
031 |
public static final int DID_ERROR = 1; |
032 |
public static final int DID_SUCCEED = 2; |
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; |
043 |
private CallbackListener listener; |
045 |
private HttpClient httpClient; |
047 |
// public HttpConnection() { |
048 |
// this(new Handler()); |
051 |
public void create(int method, String url, String data, CallbackListener listener) { |
052 |
this.method = method; |
055 |
this.listener = listener; |
056 |
ConnectionManager.getInstance().push(this); |
059 |
public void get(String url) { |
060 |
create(GET, url, null, listener); |
063 |
public void post(String url, String data, CallbackListener listener) { |
064 |
create(POST, url, data, listener); |
067 |
public void put(String url, String data) { |
068 |
create(PUT, url, data, listener); |
071 |
public void delete(String url) { |
072 |
create(DELETE, url, null, listener); |
075 |
public void bitmap(String url) { |
076 |
create(BITMAP, url, null, listener); |
079 |
public interface CallbackListener { |
080 |
public void callBack(String result); |
083 |
private static final Handler handler = new Handler() { |
085 |
public void handleMessage(Message message) { |
086 |
switch (message.what) { |
087 |
case HttpConnection.DID_START: { |
090 |
case HttpConnection.DID_SUCCEED: { |
091 |
CallbackListener listener = (CallbackListener) message.obj; |
092 |
Object data = message.getData(); |
093 |
if (listener != null) { |
095 |
Bundle bundle = (Bundle)data; |
096 |
String result = bundle.getString("callbackkey"); |
097 |
listener.callBack(result); |
102 |
case HttpConnection.DID_ERROR: { |
110 |
// handler.sendMessage(Message.obtain(handler, HttpConnection.DID_START)); |
111 |
httpClient = getHttpClient(); |
113 |
HttpResponse httpResponse = null; |
116 |
httpResponse = httpClient.execute(new HttpGet( |
117 |
StaticInfos.Server_URL + url)); |
120 |
HttpPost httpPost = new HttpPost(StaticInfos.Server_URL |
122 |
List<NameValuePair> params = new ArrayList<NameValuePair>(); |
123 |
BasicNameValuePair valuesPair = new BasicNameValuePair("args", |
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 |
131 |
this.sendMessage(result); |
133 |
this.sendMessage("fail"); |
137 |
} catch (Exception e) { |
138 |
this.sendMessage("fail"); |
140 |
ConnectionManager.getInstance().didComplete(this); |
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)); |
149 |
private void sendMessage(String result) { |
150 |
Message message = Message.obtain(handler, DID_SUCCEED, |
152 |
Bundle data = new Bundle(); |
153 |
data.putString("callbackkey", result); |
154 |
message.setData(data); |
155 |
handler.sendMessage(message); |
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); |
165 |
DefaultHttpClient httpClient = new DefaultHttpClient(httpParams); |
169 |
public static boolean isHttpSuccessExecuted(HttpResponse response) { |
170 |
int statusCode = response.getStatusLine().getStatusCode(); |
171 |
return (statusCode > 199) && (statusCode < 400); |
2 ConnectionManager类,将线程添加到队列中
01 |
package com.juupoo.common; |
04 |
import java.util.ArrayList; |
07 |
* Simple connection manager to throttle connections |
11 |
public class ConnectionManager { |
13 |
public static final int MAX_CONNECTIONS = 5; |
15 |
private ArrayList<Runnable> active = new ArrayList<Runnable>(); |
16 |
private ArrayList<Runnable> queue = new ArrayList<Runnable>(); |
18 |
private static ConnectionManager instance; |
20 |
public static ConnectionManager getInstance() { |
22 |
instance = new ConnectionManager(); |
26 |
public void push(Runnable runnable) { |
28 |
if (active.size() < MAX_CONNECTIONS) |
32 |
private void startNext() { |
33 |
if (!queue.isEmpty()) { |
34 |
Runnable next = queue.get(0); |
38 |
Thread thread = new Thread(next); |
43 |
public void didComplete(Runnable runnable) { |
44 |
active.remove(runnable); |
3 调用:
01 |
new HttpConnection().post("user.login", args, callbackListener); |
03 |
private CallbackListener callbackListener = newHttpConnection.CallbackListener() { |
05 |
public void callBack(String v) { |
07 |
if("false".equals(v)) { |
08 |
LoginActivity.this.showInfo(R.string.username_or_pass_error); |
11 |
Intent intent = new Intent(); |
12 |
intent.setClass(LoginActivity.this, MainActivity.class); |
13 |
LoginActivity.this.startActivity(intent); |
16 |
LoginActivity.this.showInfo(R.string.network_transfer_error); |
18 |
progressDialog.dismiss(); |
可参考本文。
http://masl.cis.gvsu.edu/2010/04/05/android-code-sample-asynchronous-http-connections/