2012-04-09 44 views
0

我有一个基于php的web服务,用JSON产生一堆数据。为了讨论,我在这里指的是65KB的数据样本。 65KB不应该花太多时间下载。我在一个JavaScript上测试了它(通过AJAX调用它),并且花费了几毫秒来获取数据包。我在服务器端和Android客户端添加了代码,用于标记发送和接收数据的时间戳。 令我惊讶的是,它花了2分钟的时间让Android模拟器在本地主机上取65KBAndroid HTTP连接性能

下面是我用来获取数据的方法。两个类,一个用于排队HTTP连接,并使用线程和其他线程逐个运行它们,以实际发送和接收数据以及触发处理程序的方法。

HTTPConnection.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)); 
} 

} 

ConnectionManager.java:

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); 
} 

回答

1

您正在以低效的方式将输入流处理为字符串。尝试使用StringBuilder来代替。这样的事情应该是更快的(也是测试的设备如果可能)

StringBuilder sb = new StringBuilder(); 
String line = null; 
while ((line = reader.readLine()) != null) { 
    sb.append(line + "\r\n"); 
} 

我也建议寻找到的许多方式的Android(JSON,GSON,杰克逊JSON)来解析JSON一个代替将数据作为字符串处理。

+0

我会这样做,让你知道。顺便说一句,这个问题不解析,因为我跟踪了两端(客户端和服务器)上的数据接收和发送时间戳记日志。它可能在你指出的字符串建筑中。感谢人 – kishu27 2012-04-10 03:10:06

+0

这工作。真棒。谢谢。我现在在3秒左右就可以完成整个任务。我现在正在缩短数据包的长度,甚至进一步加快速度。 – kishu27 2012-04-10 04:05:03

+1

不用担心队友。另外,我推荐JSON库的唯一原因是因为您可以从InputStream直接转到Object,从而跳过将字符串完全转换为String的步骤。如果表现是关键,我个人最喜欢的是杰克逊杰森。 – denizmveli 2012-04-10 04:11:47