2012-04-09 47 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