2
2
3
3
import android .os .AsyncTask ;
4
4
import com .qiniu .conf .Conf ;
5
+ import com .qiniu .utils .ICancel ;
6
+ import org .apache .http .Header ;
5
7
import org .apache .http .HttpEntity ;
6
8
import org .apache .http .HttpResponse ;
7
9
import org .apache .http .client .HttpClient ;
10
+ import org .apache .http .client .methods .HttpEntityEnclosingRequestBase ;
11
+ import org .apache .http .client .methods .HttpGet ;
8
12
import org .apache .http .client .methods .HttpPost ;
13
+ import org .apache .http .client .methods .HttpRequestBase ;
9
14
import org .apache .http .conn .ClientConnectionManager ;
10
15
import org .apache .http .conn .scheme .PlainSocketFactory ;
11
16
import org .apache .http .conn .scheme .Scheme ;
19
24
import java .io .IOException ;
20
25
21
26
public class Client {
22
-
27
+
23
28
protected HttpClient mClient ;
24
-
29
+
25
30
public Client (HttpClient client ) {
26
31
mClient = client ;
27
32
}
28
33
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 );
32
37
}
33
38
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 );
36
46
}
37
47
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 ) {
39
49
HttpPost httppost = new HttpPost (url );
40
50
httppost .setEntity (entity );
41
51
42
52
if (contentType != null ) {
43
53
httppost .setHeader ("Content-Type" , contentType );
44
54
}
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 );
46
60
}
47
61
48
- protected void execute ( HttpPost httpPost , CallRet ret ) {
49
- new ClientExecuter (). execute ( httpPost , ret );
62
+ public ClientExecutor makeClientExecutor ( ) {
63
+ return new ClientExecutor ( );
50
64
}
51
65
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 );
55
75
}
56
76
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
+ }
60
87
61
88
@ Override
62
89
protected Object doInBackground (Object ... objects ) {
63
- httpPost = (HttpPost ) objects [0 ];
64
- ret = (CallRet ) objects [1 ];
65
- String errMsg = "" ;
66
-
67
- HttpResponse resp ;
68
90
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 ;
70
109
} catch (IOException e ) {
71
110
e .printStackTrace ();
72
111
return e ;
73
112
}
113
+ }
74
114
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 ]);
93
118
}
94
119
95
120
@ Override
96
121
protected void onPostExecute (Object o ) {
97
122
if (o instanceof Exception ) {
98
- ret .onFailure ((Exception ) o );
123
+ mRet .onFailure ((Exception ) o );
99
124
return ;
100
125
}
101
- ret .onSuccess ((byte []) o );
126
+ mRet .onSuccess ((byte []) o );
102
127
}
103
128
};
104
129
@@ -107,11 +132,10 @@ public static Client defaultClient() {
107
132
}
108
133
109
134
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 );
115
139
return client ;
116
140
}
117
141
}
0 commit comments