package com.westudio.android.sdk.loopj.android.http;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.protocol.HttpContext;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

public class AsyncHttpRequest implements Runnable {

    private final AbstractHttpClient httpClient;
    private final HttpUriRequest request;
    private final HttpContext httpContext;
    private final AsyncHttpResponseHandler responseHandler;

    private boolean isCancelled = false;
    private boolean isFinished = false;

    public AsyncHttpRequest(AbstractHttpClient httpClient, HttpUriRequest request, HttpContext httpContext, AsyncHttpResponseHandler responseHandler) {
        this.httpClient = httpClient;
        this.request = request;
        this.httpContext = httpContext;
        this.responseHandler = responseHandler;
    }

    @Override
    public void run() {
        if (isCancelled()) {
            return;
        }

        if (responseHandler != null) {
        }

        if (isCancelled()) {
            return;
        }

        makeRequestWithExceptionsHandling();

        if (isCancelled()) {
            return;
        }

        if (responseHandler != null)
            responseHandler.sendFinishMessage();

        isFinished = true;
    }

    private void makeRequest() throws IOException {
        if (isCancelled()) {
            return;
        }

        if (request.getURI().getScheme() == null) {
            throw  new MalformedURLException("No valid URL schema was provided");
        }

        HttpResponse response = httpClient.execute(request, httpContext);

        if (!isCancelled() && responseHandler != null) {
            responseHandler.sendResponseMessage(response);
        }
    }

    private void makeRequestWithExceptionsHandling() {
        try {
            makeRequest();
            return;
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (SocketException e) {

        } catch (SocketTimeoutException e) {

        } catch (IOException e) {

        } catch (NullPointerException e) {

        } catch (Throwable t) {

        }
    }

    public boolean isCancelled() {
        if (isCancelled) {

        }

        return isCancelled;
    }

    public boolean isFinished() {
        return isCancelled || isFinished;
    }
}

@@ -271,47 +271,8 @@
 on;%0A
-import java.net.MalformedURLException;%0A
 impo
@@ -631,90 +631,8 @@
 r;%0A%0A
-    private boolean isCancelled = false;%0A    private boolean isFinished = false;%0A%0A
     
@@ -994,131 +994,151 @@
     
-if (isCancelled()) %7B
+makeRequestWithExceptionsHandling();
 %0A    
+%7D%0A%0A
     
-    return;%0A        %7D%0A%0A        if (responseHandler != null) %7B%0A        %7D%0A%0A        if (isCancell
+private void makeRequest() throws IOException %7B%0A        if (!Thread.currentThread().isInterrupt
 ed()
@@ -1145,39 +1145,37 @@
 ) %7B%0A            
-return;
+try %7B
 %0A        %7D%0A%0A    
@@ -1167,27 +1167,24 @@
         
-%7D%0A%0A
         
 makeRequ
@@ -1179,54 +1179,88 @@
     
-makeRequestWithExceptionsHandling();%0A%0A
+HttpResponse response = httpClient.execute(request, httpContext);%0A      
         
 if (
@@ -1251,37 +1251,65 @@
             
+  
 if (
-isCancell
+!Thread.currentThread().isInterrupt
 ed()) %7B%0A    
@@ -1316,35 +1316,16 @@
         
-return;%0A        %7D%0A%0A
         
@@ -1328,39 +1328,32 @@
     if (response
-Handler
  != null)%0A      
@@ -1345,17 +1345,23 @@
 != null)
-%0A
+ %7B%0A    
         
@@ -1356,32 +1356,40 @@
                 
+        
 responseHandler.
@@ -1396,141 +1396,59 @@
 send
-FinishMessage();%0A%0A        isFinished = true
+ResponseMessage(response)
 ;%0A    
-%7D%0A%0A
     
-private void makeRequest() throws IOException %7B%0A        if (isCancelled()) %7B%0A
+            %7D%0A  
     
@@ -1447,39 +1447,35 @@
 %7D%0A              
-return;
+  %7D
 %0A        %7D%0A%0A    
@@ -1471,144 +1471,100 @@
     
-%7D%0A%0A        if (request.getURI().getScheme() == null) %7B%0A            throw  new MalformedURLException(%22No valid URL schema was provided%22);
+    %7D catch (IOException e) %7B%0A                if (!Thread.currentThread().isInterrupted()) %7B
 %0A   
@@ -1568,27 +1568,24 @@
         
-%7D%0A%0A
         
 HttpResp
@@ -1580,83 +1580,29 @@
     
-HttpResponse response = httpClient.execute(request, httpContext)
+    throw e
 ;%0A
-%0A
         
 if (
@@ -1601,115 +1601,31 @@
     
-if (!isCancelled() && responseHandler != null) %7B%0A            responseHandler.sendResponseMessage(response);
+        %7D%0A            %7D
 %0A   
@@ -1801,40 +1801,8 @@
 ) %7B%0A
-            e.printStackTrace();
 %0A   
@@ -2014,199 +2014,7 @@
    %7D
-%0A%0A    public boolean isCancelled() %7B%0A        if (isCancelled) %7B%0A%0A        %7D%0A%0A        return isCancelled;%0A    %7D%0A%0A    public boolean isFinished() %7B%0A        return isCancelled %7C%7C isFinished;%0A    %7D
 %0A%7D%0A

simplify the AsyncHttpRequest....