Skip to content

Commit 7a68ad5

Browse files
author
Dean Singh
committed
Added http status check
1 parent fd7edbf commit 7a68ad5

File tree

1 file changed

+61
-19
lines changed

1 file changed

+61
-19
lines changed

src/com/loopj/android/http/AsyncHttpResponseHandler.java

Lines changed: 61 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,18 @@
2323
import org.apache.http.HttpEntity;
2424
import org.apache.http.HttpResponse;
2525
import org.apache.http.StatusLine;
26-
import org.apache.http.client.HttpResponseException;
2726
import org.apache.http.entity.BufferedHttpEntity;
2827
import org.apache.http.util.EntityUtils;
2928

29+
import android.content.Intent;
3030
import android.os.Handler;
31-
import android.os.Message;
3231
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;
3338

3439
/**
3540
* Used to intercept and handle the responses from requests made using
@@ -73,6 +78,8 @@ public class AsyncHttpResponseHandler {
7378
private static final int FINISH_MESSAGE = 3;
7479

7580
private Handler handler;
81+
82+
public HttpResponse mResponse;
7683

7784
/**
7885
* Creates a new AsyncHttpResponseHandler
@@ -200,27 +207,62 @@ protected Message obtainMessage(int responseMessage, Object response) {
200207
}
201208
return msg;
202209
}
203-
204-
210+
205211
// Interface to AsyncHttpRequest
206-
void sendResponseMessage(HttpResponse response) {
212+
void sendResponseMessage(HttpResponse response)
213+
{
214+
mResponse = response;
207215
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);
214233
}
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+
}
219259

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+
}
224266
}
225267
}
226268
}

0 commit comments

Comments
 (0)