package org.adaptlab.chpir.android.activerecordcloudsync;

import java.io.InputStream;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import com.activeandroid.query.Select;

import android.os.Looper;
import android.util.Log;

public class HttpPushr {
    private static final String TAG = "HttpPushr";
    private Class<? extends SendModel> mSendTableClass;
    private String mRemoteTableName;

    public HttpPushr(String remoteTableName,
            Class<? extends SendModel> sendTableClass) {
        mSendTableClass = sendTableClass;
        mRemoteTableName = remoteTableName;
    }

    public void push() {
        if (ActiveRecordCloudSync.getEndPoint() == null) {
            Log.i(TAG, "ActiveRecordCloudSync end point is not set!");
            return;
        }

        List<? extends SendModel> allElements = new Select()
                .from(mSendTableClass).orderBy("Id ASC").execute();

        for (final SendModel element : allElements) {
            Thread t = new Thread() {
                public void run() {
                    Looper.prepare(); // For Preparing Message Pool for the
                                      // child Thread
                    HttpClient client = new DefaultHttpClient();
                    HttpConnectionParams.setConnectionTimeout(
                            client.getParams(), 10000); // Timeout Limit
                    HttpResponse response;

                    if (!element.isSent()) {
                        try {
                            HttpPost post = new HttpPost(
                                    ActiveRecordCloudSync.getEndPoint()
                                            + mRemoteTableName);
                            StringEntity se = new StringEntity(element.toJSON()
                                    .toString());
                            se.setContentType(new BasicHeader(
                                    HTTP.CONTENT_TYPE, "application/json"));
                            post.setEntity(se);
                            Log.i(TAG, "Sending post request: "
                                    + element.toJSON().toString());
                            response = client.execute(post);

                            /* Checking response */
                            if (response != null) {
                                InputStream in = response.getEntity()
                                        .getContent();
                                element.setAsSent();
                                element.save();
                            }

                        } catch (Exception e) {
                            Log.e(TAG, "Cannot establish connection", e);
                        }

                        Looper.loop(); // Loop in the message queue
                    }
                }
            };

            t.start();
        }

    }
}

@@ -2561,16 +2561,31 @@
 hecking 
+for successful 
 response
@@ -2632,19 +2632,231 @@
 onse
- != null) %7B
+.getStatusLine().getStatusCode() %3E= 200 &&%0A                                    response.getStatusLine().getStatusCode() %3C 300) %7B%0A                                Log.i(TAG, %22Received OK HTTP status for %22 + element.toJSON());
 %0A   

Check that HTTP response is OK before setting response as saved