Android HTTP连接性能
我有一个基于php的web服务,它用JSON生成大量数据。为了便于讨论,我这里指的是一个65KB的数据示例。65KB应该不需要太多的下载。我在一个JavaScript(用AJAX调用)上测试了这一点,获取数据包需要几毫秒的时间。我在服务器端和android客户端添加了代码,这些代码将标记数据发送和接收的时间戳令我恐惧的是,即使在本地主机上,Android模拟器也需要2分钟才能获取65KB的内存 下面是我用来获取数据的方法。两个类,一个用于排队HTTP连接并使用线程逐个运行它们,另一个用于实际发送和接收数据以及处理程序的触发方法 HTTPConnection.java:Android HTTP连接性能,android,android-networking,Android,Android Networking,我有一个基于php的web服务,它用JSON生成大量数据。为了便于讨论,我这里指的是一个65KB的数据示例。65KB应该不需要太多的下载。我在一个JavaScript(用AJAX调用)上测试了这一点,获取数据包需要几毫秒的时间。我在服务器端和android客户端添加了代码,这些代码将标记数据发送和接收的时间戳令我恐惧的是,即使在本地主机上,Android模拟器也需要2分钟才能获取65KB的内存 下面是我用来获取数据的方法。两个类,一个用于排队HTTP连接并使用线程逐个运行它们,另一个用于实际发送
public class HttpConnection implements Runnable
{
public static final int DID_START = 0;
public static final int DID_ERROR = 1;
public static final int DID_SUCCEED = 2;
private static final int GET = 0;
private static final int POST = 1;
private static final int PUT = 2;
private static final int DELETE = 3;
private static final int BITMAP = 4;
private String url;
private int method;
private Handler handler;
private List<NameValuePair> postData;
private String data;
private HttpClient httpClient;
public HttpConnection()
{
this(new Handler());
}
public HttpConnection(Handler _handler)
{
handler = _handler;
}
public void create(int method, String url, String data)
{
this.method = method;
this.url = url;
this.data = data;
ConnectionManager.getInstance().push(this);
}
public void createPost(int method, String url, List<NameValuePair>data)
{
this.method = method;
this.url = url;
this.postData = data;
ConnectionManager.getInstance().push(this);
}
public void get(String url)
{
create(GET, url, null);
}
public void post(String url, List<NameValuePair> data)
{
createPost(POST, url, data);
}
public void put(String url, String data)
{
create(PUT, url, data);
}
public void delete(String url)
{
create(DELETE, url, null);
}
public void bitmap(String url)
{
create(BITMAP, url, null);
}
public void run()
{
handler.sendMessage(Message.obtain(handler, HttpConnection.DID_START));
httpClient = new DefaultHttpClient();
HttpConnectionParams.setSoTimeout(httpClient.getParams(), 10000);
try {
HttpResponse response = null;
switch (method) {
case GET:
response = httpClient.execute(new HttpGet(url));
break;
case POST:
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(postData));
response = httpClient.execute(httpPost);
break;
case PUT:
HttpPut httpPut = new HttpPut(url);
httpPut.setEntity(new StringEntity(data));
response = httpClient.execute(httpPut);
break;
case DELETE:
response = httpClient.execute(new HttpDelete(url));
break;
case BITMAP:
response = httpClient.execute(new HttpGet(url));
processBitmapEntity(response.getEntity());
break;
}
if (method < BITMAP)
processEntity(response.getEntity());
} catch (Exception e) {
handler.sendMessage(Message.obtain(handler,
HttpConnection.DID_ERROR, e));
}
ConnectionManager.getInstance().didComplete(this);
}
private void processEntity(HttpEntity entity) throws IllegalStateException,IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
String line, result = "";
while ((line = br.readLine()) != null)
result += line;
Message message = Message.obtain(handler, DID_SUCCEED, result);
handler.sendMessage(message);
}
private void processBitmapEntity(HttpEntity entity) throws IOException
{
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
Bitmap bm = BitmapFactory.decodeStream(bufHttpEntity.getContent());
handler.sendMessage(Message.obtain(handler, DID_SUCCEED, bm));
}
}
public class ConnectionManager
{
public static final int MAX_CONNECTIONS = 5;
private ArrayList<Runnable> active = new ArrayList<Runnable>();
private ArrayList<Runnable> queue = new ArrayList<Runnable>();
private static ConnectionManager instance;
public static ConnectionManager getInstance()
{
if (instance == null)
instance = new ConnectionManager();
return instance;
}
public void push(Runnable runnable)
{
queue.add(runnable);
if (active.size() < MAX_CONNECTIONS)
startNext();
}
private void startNext()
{
if (!queue.isEmpty())
{
Runnable next = queue.get(0);
queue.remove(0);
active.add(next);
Thread thread = new Thread(next);
thread.start();
}
}
public void didComplete(Runnable runnable)
{
active.remove(runnable);
startNext();
}
}
public void SendHTTPRequest(String function, List<NameValuePair> Data, Handler handler)
{
new HttpConnection(handler).post(this.getServerAddress() + "/service-endpoint.php?function="+function,Data);
}
公共类HttpConnection实现可运行
{
公共静态最终int DID_START=0;
公共静态最终int DID_错误=1;
公共静态最终int DID_SUCCESS=2;
私有静态final int GET=0;
专用静态最终int POST=1;
专用静态最终int PUT=2;
私有静态最终整数删除=3;
私有静态最终整型位图=4;
私有字符串url;
私有int方法;
私人经办人;
私有列表postData;
私有字符串数据;
私有HttpClient HttpClient;
公共HttpConnection()
{
此(新处理程序());
}
公共HttpConnection(处理程序_处理程序)
{
handler=\u handler;
}
公共void创建(int方法、字符串url、字符串数据)
{
这个方法=方法;
this.url=url;
这个数据=数据;
ConnectionManager.getInstance().push(这个);
}
public void createPost(int方法、字符串url、Listdata)
{
这个方法=方法;
this.url=url;
this.postData=数据;
ConnectionManager.getInstance().push(这个);
}
公共void get(字符串url)
{
创建(GET、url、null);
}
公共作废帖子(字符串url、列表数据)
{
createPost(帖子、url、数据);
}
公共void put(字符串url、字符串数据)
{
创建(放置、url、数据);
}
公共无效删除(字符串url)
{
创建(删除,url,空);
}
公共无效位图(字符串url)
{
创建(位图、url、空);
}
公开募捐
{
sendMessage(Message.get(handler,HttpConnection.DID_START));
httpClient=新的DefaultHttpClient();
HttpConnectionParams.setSoTimeout(httpClient.getParams(),10000);
试一试{
HttpResponse响应=null;
开关(方法){
案例获取:
response=httpClient.execute(新的HttpGet(url));
打破
个案职位:
HttpPost HttpPost=新的HttpPost(url);
setEntity(新的UrlEncodedFormEntity(postData));
response=httpClient.execute(httpPost);
打破
案件付诸表决:
HttpPut HttpPut=新的HttpPut(url);
setEntity(新的StringEntity(数据));
response=httpClient.execute(httpPut);
打破
案例删除:
response=httpClient.execute(新的HttpDelete(url));
打破
大小写位图:
response=httpClient.execute(新的HttpGet(url));
processBitmapEntity(response.getEntity());
打破
}
if(方法<位图)
processEntity(response.getEntity());
}捕获(例外e){
handler.sendMessage(Message.get(handler,
HttpConnection.DID_错误,e));
}
ConnectionManager.getInstance().didComplete(此);
}
私有void processEntity(HttpEntity entity)抛出IllegalStateException、IOException
{
BufferedReader br=新的BufferedReader(新的InputStreamReader(entity.getContent());
字符串行,结果=”;
而((line=br.readLine())!=null)
结果+=行;
Message Message=Message.get(处理程序,是否成功,结果);
handler.sendMessage(message);
}
私有void processBitmapEntity(HttpEntity实体)引发IOException
{
BufferedHttpEntity bufHttpEntity=新的BufferedHttpEntity(实体);
位图bm=BitmapFactory.decodeStream(bufhttpenty.getContent());
handler.sendMessage(Message.get(handler,DID_success,bm));
}
}
ConnectionManager.java:
public class HttpConnection implements Runnable
{
public static final int DID_START = 0;
public static final int DID_ERROR = 1;
public static final int DID_SUCCEED = 2;
private static final int GET = 0;
private static final int POST = 1;
private static final int PUT = 2;
private static final int DELETE = 3;
private static final int BITMAP = 4;
private String url;
private int method;
private Handler handler;
private List<NameValuePair> postData;
private String data;
private HttpClient httpClient;
public HttpConnection()
{
this(new Handler());
}
public HttpConnection(Handler _handler)
{
handler = _handler;
}
public void create(int method, String url, String data)
{
this.method = method;
this.url = url;
this.data = data;
ConnectionManager.getInstance().push(this);
}
public void createPost(int method, String url, List<NameValuePair>data)
{
this.method = method;
this.url = url;
this.postData = data;
ConnectionManager.getInstance().push(this);
}
public void get(String url)
{
create(GET, url, null);
}
public void post(String url, List<NameValuePair> data)
{
createPost(POST, url, data);
}
public void put(String url, String data)
{
create(PUT, url, data);
}
public void delete(String url)
{
create(DELETE, url, null);
}
public void bitmap(String url)
{
create(BITMAP, url, null);
}
public void run()
{
handler.sendMessage(Message.obtain(handler, HttpConnection.DID_START));
httpClient = new DefaultHttpClient();
HttpConnectionParams.setSoTimeout(httpClient.getParams(), 10000);
try {
HttpResponse response = null;
switch (method) {
case GET:
response = httpClient.execute(new HttpGet(url));
break;
case POST:
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(postData));
response = httpClient.execute(httpPost);
break;
case PUT:
HttpPut httpPut = new HttpPut(url);
httpPut.setEntity(new StringEntity(data));
response = httpClient.execute(httpPut);
break;
case DELETE:
response = httpClient.execute(new HttpDelete(url));
break;
case BITMAP:
response = httpClient.execute(new HttpGet(url));
processBitmapEntity(response.getEntity());
break;
}
if (method < BITMAP)
processEntity(response.getEntity());
} catch (Exception e) {
handler.sendMessage(Message.obtain(handler,
HttpConnection.DID_ERROR, e));
}
ConnectionManager.getInstance().didComplete(this);
}
private void processEntity(HttpEntity entity) throws IllegalStateException,IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
String line, result = "";
while ((line = br.readLine()) != null)
result += line;
Message message = Message.obtain(handler, DID_SUCCEED, result);
handler.sendMessage(message);
}
private void processBitmapEntity(HttpEntity entity) throws IOException
{
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
Bitmap bm = BitmapFactory.decodeStream(bufHttpEntity.getContent());
handler.sendMessage(Message.obtain(handler, DID_SUCCEED, bm));
}
}
public class ConnectionManager
{
public static final int MAX_CONNECTIONS = 5;
private ArrayList<Runnable> active = new ArrayList<Runnable>();
private ArrayList<Runnable> queue = new ArrayList<Runnable>();
private static ConnectionManager instance;
public static ConnectionManager getInstance()
{
if (instance == null)
instance = new ConnectionManager();
return instance;
}
public void push(Runnable runnable)
{
queue.add(runnable);
if (active.size() < MAX_CONNECTIONS)
startNext();
}
private void startNext()
{
if (!queue.isEmpty())
{
Runnable next = queue.get(0);
queue.remove(0);
active.add(next);
Thread thread = new Thread(next);
thread.start();
}
}
public void didComplete(Runnable runnable)
{
active.remove(runnable);
startNext();
}
}
public void SendHTTPRequest(String function, List<NameValuePair> Data, Handler handler)
{
new HttpConnection(handler).post(this.getServerAddress() + "/service-endpoint.php?function="+function,Data);
}
公共类连接管理器
{
公共静态最终int最大连接数=5;
private ArrayList active=新的ArrayList();
私有ArrayList队列=新建ArrayList();
私有静态连接管理器实例;
公共静态连接管理器getInstance()
{
if(实例==null)
实例=新的ConnectionManager();
返回实例;
}
公共无效推送(可运行可运行)
{
queue.add(可运行);
if(活动的.size()
这些代码被称为:
public class HttpConnection implements Runnable
{
public static final int DID_START = 0;
public static final int DID_ERROR = 1;
public static final int DID_SUCCEED = 2;
private static final int GET = 0;
private static final int POST = 1;
private static final int PUT = 2;
private static final int DELETE = 3;
private static final int BITMAP = 4;
private String url;
private int method;
private Handler handler;
private List<NameValuePair> postData;
private String data;
private HttpClient httpClient;
public HttpConnection()
{
this(new Handler());
}
public HttpConnection(Handler _handler)
{
handler = _handler;
}
public void create(int method, String url, String data)
{
this.method = method;
this.url = url;
this.data = data;
ConnectionManager.getInstance().push(this);
}
public void createPost(int method, String url, List<NameValuePair>data)
{
this.method = method;
this.url = url;
this.postData = data;
ConnectionManager.getInstance().push(this);
}
public void get(String url)
{
create(GET, url, null);
}
public void post(String url, List<NameValuePair> data)
{
createPost(POST, url, data);
}
public void put(String url, String data)
{
create(PUT, url, data);
}
public void delete(String url)
{
create(DELETE, url, null);
}
public void bitmap(String url)
{
create(BITMAP, url, null);
}
public void run()
{
handler.sendMessage(Message.obtain(handler, HttpConnection.DID_START));
httpClient = new DefaultHttpClient();
HttpConnectionParams.setSoTimeout(httpClient.getParams(), 10000);
try {
HttpResponse response = null;
switch (method) {
case GET:
response = httpClient.execute(new HttpGet(url));
break;
case POST:
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(postData));
response = httpClient.execute(httpPost);
break;
case PUT:
HttpPut httpPut = new HttpPut(url);
httpPut.setEntity(new StringEntity(data));
response = httpClient.execute(httpPut);
break;
case DELETE:
response = httpClient.execute(new HttpDelete(url));
break;
case BITMAP:
response = httpClient.execute(new HttpGet(url));
processBitmapEntity(response.getEntity());
break;
}
if (method < BITMAP)
processEntity(response.getEntity());
} catch (Exception e) {
handler.sendMessage(Message.obtain(handler,
HttpConnection.DID_ERROR, e));
}
ConnectionManager.getInstance().didComplete(this);
}
private void processEntity(HttpEntity entity) throws IllegalStateException,IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
String line, result = "";
while ((line = br.readLine()) != null)
result += line;
Message message = Message.obtain(handler, DID_SUCCEED, result);
handler.sendMessage(message);
}
private void processBitmapEntity(HttpEntity entity) throws IOException
{
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
Bitmap bm = BitmapFactory.decodeStream(bufHttpEntity.getContent());
handler.sendMessage(Message.obtain(handler, DID_SUCCEED, bm));
}
}
public class ConnectionManager
{
public static final int MAX_CONNECTIONS = 5;
private ArrayList<Runnable> active = new ArrayList<Runnable>();
private ArrayList<Runnable> queue = new ArrayList<Runnable>();
private static ConnectionManager instance;
public static ConnectionManager getInstance()
{
if (instance == null)
instance = new ConnectionManager();
return instance;
}
public void push(Runnable runnable)
{
queue.add(runnable);
if (active.size() < MAX_CONNECTIONS)
startNext();
}
private void startNext()
{
if (!queue.isEmpty())
{
Runnable next = queue.get(0);
queue.remove(0);
active.add(next);
Thread thread = new Thread(next);
thread.start();
}
}
public void didComplete(Runnable runnable)
{
active.remove(runnable);
startNext();
}
}
public void SendHTTPRequest(String function, List<NameValuePair> Data, Handler handler)
{
new HttpConnection(handler).post(this.getServerAddress() + "/service-endpoint.php?function="+function,Data);
}
public void SendHTTPRequest(字符串函数、列表数据、处理程序)
{
新的HttpConnection(handler).post(this.getServerAddress()+”/service endpoint.php?function=“+函数,数据);
}
您正在以低效的方式将输入流处理为字符串。尝试改用StringBuilder。类似的东西应该更快(如果可能,也可以在设备上进行测试)
我还建议研究Android中解析JSON的多种方法之一(JSON、GSON、Jackson JSON),而不是将数据作为字符串处理。我会这样做,并让您知道。顺便说一句,问题不在于解析,因为我跟踪了两端(客户端和服务器)数据接收和发送的时间戳日志。它可能像你指的那样在弦楼里。谢谢,这很有效。令人惊叹的。谢谢我现在可以在3秒钟内拿到所有的东西。我现在正在缩短数据包的长度,以便进一步加快速度