Android has two sets of HTTP APIs, just beginning to use network programming is somewhat confusing in the end with which good? In fact, there's no need to argue, but Google has pointed out that httpurlconnection is a better choice for Android and that it references blogs in the SDK documentation (requiring proxy access) http:// Android-developers.blogspot.com/2011/09/androids-http-clients.html to illustrate their strengths and weaknesses. Some of the domestic blog translation of the above content, and on some tests, unfortunately the test is not tight, a blog even to get httpurlconnection download speed several times the conclusion, in fact, not fair reflects the download speed of both.
Although I have used httpurlconnection to implement a lightweight HTTP engine for downloading, Curiosity has prompted me to write a test program to compare the performance of both. Because HttpClient is only an interface, so I chose its implementation class defaulthttpclient and httpurlconnection do comparisons, the method is very simple, download the same file 10 times, and then calculate the time-consuming average, the test code fragment is as follows:
@Override public void OnClick (View v) {if (V.equals (MTESTHTTPCLIENTBTN)) {new Threa
D (New Runnable () {@Override public void run () {
Long averagetime = 0; for (int i = 0; i < i++) {File File = new file (Getfilesdir (), STRING.V
Alueof (i) + ". File");
FileOutputStream fileoutputstream = null;
try {fileoutputstream = new FileOutputStream (file); catch (FileNotFoundException e) {Lo
G.E (TAG, "", e);
Return
Long starttime = System.currenttimemillis (); Testhttpclient (fileoutputstreAM);
Long stoptime = System.currenttimemillis ();
Averagetime + = Stoptime-starttime;
} averagetime/= 10;
Test completion message msg = new Message ();
Msg.what = Msg_test_http_client_done;
Msg.obj = Averagetime;
Mhandler.sendmessage (msg);
}). Start ();
Return
} if (V.equals (MTESTHTTPURLCONNECTIONBTN)) {New Thread (new Runnable ()) {@Override public void run () {Long averagetime = 0
; for (int i = 0; i < i++) {File File = new file (Getfilesdir (), STRING.V
Alueof (i +) + ". File");FileOutputStream fileoutputstream = null;
try {fileoutputstream = new FileOutputStream (file); catch (FileNotFoundException e) {Lo
G.E (TAG, "", e);
Return
Long starttime = System.currenttimemillis ();
Testhttpurlconnection (FileOutputStream);
Long stoptime = System.currenttimemillis ();
Averagetime + = Stoptime-starttime;
} averagetime/= 10; Test completion//Return to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/Message msg = new Message
(); Msg.what = Msg_test_http_url_connection_done;
Msg.obj = Averagetime;
Mhandler.sendmessage (msg);
}). Start ();
Return }} private void Testhttpclient (FileOutputStream fileoutputstream) {defaulthttpclient HTTPCL
ient = new Defaulthttpclient ();
Httpentity entity = NULL;
InputStream inputstream = null;
try {httpget httpget = new HttpGet (Test_url);
HttpResponse HttpResponse = Httpclient.execute (HttpGet);
Statusline statusline = Httpresponse.getstatusline ();
if (Statusline = = null) {throw new Exception ("No status line!!!");
int responsecode = Statusline.getstatuscode (); if (Responsecode < | | Responsecode >=) {throw new Exception ("Response Error!!
!");
} EnTity = Httpresponse.getentity ();
if (entity = = null) {throw new Exception ("no entity!!!");
} InputStream = Entity.getcontent ();
int bytesread =-1;
byte[] buffer = new byte[4096]; while ((bytesread = inputstream.read (buffer))!=-1) {fileoutputstream.write (buffer, 0, bytes
Read);
The catch (Exception e) {log.e (TAG, "", e);
Finally {try {if (InputStream!= null) {
Inputstream.close ();
} if (entity!= null) {entity.consumecontent ();
} if (FileOutputStream!= null) {
Fileoutputstream.flush (); Fileoutputstream.close ();
The catch (Exception e) {log.e (TAG, "", e); }} private void Testhttpurlconnection (FileOutputStream fileoutputstream) {Httpurl
Connection httpurlconnection = null;
InputStream inputstream = null;
try {httpurlconnection = (httpurlconnection) new URL (Test_url). OpenConnection ();
Httpurlconnection.setrequestmethod ("get");
Httpurlconnection.connect ();
int responsecode = Httpurlconnection.getresponsecode (); if (Responsecode < | | Responsecode >=) {throw new Exception ("Response Error!!
!");
}
InputStream = Httpurlconnection.getinputstream ();
int bytesread =-1;
byte[] buffer = new byte[4096];
while ((bytesread = inputstream.read (buffer))!=-1) { Fileoutputstream.write (buffer, 0, bytesread);
The catch (Exception e) {log.e (TAG, "", e);
Finally {try {if (InputStream!= null) {
Inputstream.close (); } if (httpurlconnection!= null) {Httpurlconnect
Ion.disconnect (); } if (FileOutputStream!= null) {Fileoutputstrea
M.flush ();
Fileoutputstream.close ();
The catch (Exception e) {log.e (TAG, "", e); }
}
}