23
23
import org .apache .http .HttpEntity ;
24
24
import org .apache .http .HttpResponse ;
25
25
import org .apache .http .StatusLine ;
26
- import org .apache .http .client .HttpResponseException ;
27
26
import org .apache .http .entity .BufferedHttpEntity ;
28
27
import org .apache .http .util .EntityUtils ;
29
28
29
+ import android .content .Intent ;
30
30
import android .os .Handler ;
31
- import android .os .Message ;
32
31
import android .os .Looper ;
32
+ import android .os .Message ;
33
+
34
+ import com .pose .PoseApp ;
35
+ import com .pose .SignupSplashActivity ;
36
+ import com .pose .models .User ;
37
+ import com .pose .utility .GSGlobals ;
33
38
34
39
/**
35
40
* Used to intercept and handle the responses from requests made using
@@ -73,6 +78,8 @@ public class AsyncHttpResponseHandler {
73
78
private static final int FINISH_MESSAGE = 3 ;
74
79
75
80
private Handler handler ;
81
+
82
+ public HttpResponse mResponse ;
76
83
77
84
/**
78
85
* Creates a new AsyncHttpResponseHandler
@@ -200,27 +207,62 @@ protected Message obtainMessage(int responseMessage, Object response) {
200
207
}
201
208
return msg ;
202
209
}
203
-
204
-
210
+
205
211
// Interface to AsyncHttpRequest
206
- void sendResponseMessage (HttpResponse response ) {
212
+ void sendResponseMessage (HttpResponse response )
213
+ {
214
+ mResponse = response ;
207
215
StatusLine status = response .getStatusLine ();
208
- String responseBody = null ;
209
- try {
210
- HttpEntity entity = null ;
211
- HttpEntity temp = response .getEntity ();
212
- if (temp != null ) {
213
- entity = new BufferedHttpEntity (temp );
216
+ if (status .getStatusCode () >= 300 )
217
+ {
218
+ //sendFailureMessage(new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()));
219
+ try
220
+ {
221
+ HttpEntity entity = null ;
222
+ HttpEntity temp = response .getEntity ();
223
+ if (temp != null )
224
+ {
225
+ entity = new BufferedHttpEntity (temp );
226
+ }
227
+
228
+ sendFailureMessage (null , EntityUtils .toString (entity ));
229
+ }
230
+ catch (IOException e )
231
+ {
232
+ sendFailureMessage (e , null );
214
233
}
215
- responseBody = EntityUtils .toString (entity );
216
- } catch (IOException e ) {
217
- sendFailureMessage (e , null );
218
- }
234
+
235
+ if (status .getStatusCode () == 401 )
236
+ {
237
+ if (User .isAuthenticated ())
238
+ {
239
+ User .signout (null );
240
+ PoseApp .clearDB ();
241
+ Intent intentSendLogout = new Intent (GSGlobals .INTENT_POSE_LOGOUT );
242
+ PoseApp .getContext ().sendBroadcast (intentSendLogout );
243
+
244
+ Intent intent = new Intent (PoseApp .getContext (), SignupSplashActivity .class );
245
+ intent .addFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
246
+ PoseApp .getContext ().startActivity (intent );
247
+ }
248
+ }
249
+ } else
250
+ {
251
+ try
252
+ {
253
+ HttpEntity entity = null ;
254
+ HttpEntity temp = response .getEntity ();
255
+ if (temp != null )
256
+ {
257
+ entity = new BufferedHttpEntity (temp );
258
+ }
219
259
220
- if (status .getStatusCode () >= 300 ) {
221
- sendFailureMessage (new HttpResponseException (status .getStatusCode (), status .getReasonPhrase ()), responseBody );
222
- } else {
223
- sendSuccessMessage (responseBody );
260
+ sendSuccessMessage (EntityUtils .toString (entity ));
261
+ }
262
+ catch (IOException e )
263
+ {
264
+ sendFailureMessage (e , null );
265
+ }
224
266
}
225
267
}
226
268
}
0 commit comments