22
33import android .os .AsyncTask ;
44import com .qiniu .conf .Conf ;
5+ import com .qiniu .utils .ICancel ;
6+ import org .apache .http .Header ;
57import org .apache .http .HttpEntity ;
68import org .apache .http .HttpResponse ;
79import org .apache .http .client .HttpClient ;
10+ import org .apache .http .client .methods .HttpEntityEnclosingRequestBase ;
11+ import org .apache .http .client .methods .HttpGet ;
812import org .apache .http .client .methods .HttpPost ;
13+ import org .apache .http .client .methods .HttpRequestBase ;
914import org .apache .http .conn .ClientConnectionManager ;
1015import org .apache .http .conn .scheme .PlainSocketFactory ;
1116import org .apache .http .conn .scheme .Scheme ;
1924import java .io .IOException ;
2025
2126public class Client {
22-
27+
2328 protected HttpClient mClient ;
24-
29+
2530 public Client (HttpClient client ) {
2631 mClient = client ;
2732 }
2833
29- public void call (String url , CallRet ret ) {
30- HttpPost httppost = new HttpPost ( url );
31- execute ( httppost , ret );
34+ public static ClientExecutor get (String url , CallRet ret ) {
35+ Client client = Client . defaultClient ( );
36+ return client . get ( client . makeClientExecutor (), url , ret );
3237 }
3338
34- public void call (String url , HttpEntity entity , CallRet ret ) {
35- call (url , entity .getContentType ().getValue (), entity , ret );
39+ public ClientExecutor call (ClientExecutor client , String url , HttpEntity entity , CallRet ret ) {
40+ Header header = entity .getContentType ();
41+ String contentType = "application/octet-stream" ;
42+ if (header != null ) {
43+ contentType = header .getValue ();
44+ }
45+ return call (client , url , contentType , entity , ret );
3646 }
3747
38- public void call (String url , String contentType , HttpEntity entity , CallRet ret ) {
48+ public ClientExecutor call (ClientExecutor client , String url , String contentType , HttpEntity entity , CallRet ret ) {
3949 HttpPost httppost = new HttpPost (url );
4050 httppost .setEntity (entity );
4151
4252 if (contentType != null ) {
4353 httppost .setHeader ("Content-Type" , contentType );
4454 }
45- execute (httppost , ret );
55+ return execute (client , httppost , ret );
56+ }
57+
58+ public ClientExecutor get (ClientExecutor client , String url , CallRet ret ) {
59+ return execute (client , new HttpGet (url ), ret );
4660 }
4761
48- protected void execute ( HttpPost httpPost , CallRet ret ) {
49- new ClientExecuter (). execute ( httpPost , ret );
62+ public ClientExecutor makeClientExecutor ( ) {
63+ return new ClientExecutor ( );
5064 }
5165
52- protected HttpResponse roundtrip (HttpPost httpPost ) throws IOException {
53- httpPost .setHeader ("User-Agent" , Conf .USER_AGENT );
54- return mClient .execute (httpPost );
66+ protected ClientExecutor execute (ClientExecutor client , HttpRequestBase httpRequest , final CallRet ret ) {
67+ client .setup (httpRequest , ret );
68+ client .execute ();
69+ return client ;
70+ }
71+
72+ protected HttpResponse roundtrip (HttpRequestBase httpRequest ) throws IOException {
73+ httpRequest .setHeader ("User-Agent" , Conf .USER_AGENT );
74+ return mClient .execute (httpRequest );
5575 }
5676
57- class ClientExecuter extends AsyncTask <Object , Object , Object > {
58- HttpPost httpPost ;
59- CallRet ret ;
77+ public class ClientExecutor extends AsyncTask <Object , Object , Object > implements ICancel {
78+ HttpRequestBase mHttpRequest ;
79+ CallRet mRet ;
80+ public void setup (HttpRequestBase httpRequest , CallRet ret ) {
81+ mHttpRequest = httpRequest ;
82+ mRet = ret ;
83+ }
84+ public void upload (long current , long total ) {
85+ publishProgress (current , total );
86+ }
6087
6188 @ Override
6289 protected Object doInBackground (Object ... objects ) {
63- httpPost = (HttpPost ) objects [0 ];
64- ret = (CallRet ) objects [1 ];
65- String errMsg = "" ;
66-
67- HttpResponse resp ;
6890 try {
69- resp = roundtrip (httpPost );
91+ HttpResponse resp = roundtrip (mHttpRequest );
92+ int statusCode = resp .getStatusLine ().getStatusCode ();
93+ if (statusCode == 401 ) { // android 2.3 will not response
94+ return new Exception ("unauthorized!" );
95+ }
96+ byte [] data = EntityUtils .toByteArray (resp .getEntity ());
97+
98+ if (statusCode / 100 != 2 ) {
99+ if (data .length == 0 ) {
100+ String xlog = resp .getFirstHeader ("X-Log" ).getValue ();
101+ if (xlog .length () > 0 ) {
102+ return new Exception (xlog );
103+ }
104+ return new Exception (resp .getStatusLine ().getReasonPhrase ());
105+ }
106+ return new Exception (new String (data ));
107+ }
108+ return data ;
70109 } catch (IOException e ) {
71110 e .printStackTrace ();
72111 return e ;
73112 }
113+ }
74114
75- if (resp .getHeaders ("X-Log" ).length > 0 ) {
76- errMsg = resp .getHeaders ("X-Log" )[0 ].getValue ();
77- }
78-
79- int statusCode = resp .getStatusLine ().getStatusCode ();
80-
81- if (statusCode / 100 != 2 ) {
82- return new Exception (errMsg );
83- }
84-
85- byte [] data = new byte [0 ];
86- try {
87- data = EntityUtils .toByteArray (resp .getEntity ());
88- } catch (IOException e ) {
89- e .printStackTrace ();
90- }
91-
92- return data ;
115+ @ Override
116+ protected void onProgressUpdate (Object ... values ) {
117+ mRet .onProcess ((Long ) values [0 ], (Long ) values [1 ]);
93118 }
94119
95120 @ Override
96121 protected void onPostExecute (Object o ) {
97122 if (o instanceof Exception ) {
98- ret .onFailure ((Exception ) o );
123+ mRet .onFailure ((Exception ) o );
99124 return ;
100125 }
101- ret .onSuccess ((byte []) o );
126+ mRet .onSuccess ((byte []) o );
102127 }
103128 };
104129
@@ -107,11 +132,10 @@ public static Client defaultClient() {
107132 }
108133
109134 public static HttpClient getMultithreadClient () {
110- HttpParams params = new BasicHttpParams ();
111- SchemeRegistry registry = new SchemeRegistry ();
112- registry .register (new Scheme ("http" , PlainSocketFactory .getSocketFactory (), 80 ));
113- ClientConnectionManager cm = new ThreadSafeClientConnManager (params , registry );
114- HttpClient client = new DefaultHttpClient (cm , params );
135+ HttpClient client = new DefaultHttpClient ();
136+ ClientConnectionManager mgr = client .getConnectionManager ();
137+ HttpParams params = client .getParams ();
138+ client = new DefaultHttpClient (new ThreadSafeClientConnManager (params , mgr .getSchemeRegistry ()), params );
115139 return client ;
116140 }
117141}
0 commit comments