<commit_before>package com.localhop.network;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.SocketTimeoutException;
import java.util.List;

import android.app.Activity;
import android.widget.Toast;
import org.apache.http.*;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.HttpHostConnectException;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

/**
 * Task that connects to a server running HERMIT-web and simulates HERMIT commands
 * by using HTTP GET and POST requests.
 */
public abstract class HttpServerRequest<A extends Activity, Result> extends AsyncActivityTask<A, String, Void, Result> {
    public static String DUMMY_URL = "http://ip.jsontest.com/";
    private HttpRequest mRequest;
    private String mErrorMessage;
    private List<NameValuePair> mData;

    /**
     * Constructs a new instance. The constructor is not the place to put any input
     * {@link JSONObject}s (do that in {@link android.os.AsyncTask#execute(JSONObject...)
     * execute(JSONObject...)} instead).
     *
     * @param console reference to the current console.
     */
    public HttpServerRequest(A activity, HttpRequest request, List<NameValuePair> data) {
        super(activity);
        mRequest = request;
        mData = data;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Result doInBackground(String... params) {
        HttpClient httpClient = null;
        HttpResponse httpResponse = null;
        String responseStr = null;

        try {
            final HttpParams httpParams = new BasicHttpParams();
            //Set timeout length to 10 seconds
            HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
            HttpConnectionParams.setSoTimeout(httpParams, 10000);
            httpClient = new DefaultHttpClient(httpParams);
            HttpUriRequest request = null;
            if (mRequest.equals(HttpRequest.GET)) {
                request = new HttpGet(params[0]);
            } else if (mRequest.equals(HttpRequest.POST)) {
                final HttpPost httpPost = new HttpPost(params[0]);
                if (params.length > 1) {
                    httpPost.setHeader("content-type", "application/json");
                    if (params[1] != null && !params[1].isEmpty()) {
                        try {
                            httpPost.setEntity(new StringEntity(params[1]));
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                    }
                }
                request = httpPost;
            }

            if (!isCancelled()) {
                httpResponse = httpClient.execute(request);
            }

            if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                final String entity = EntityUtils.toString(httpResponse.getEntity(), HTTP.UTF_8).trim();
                if (entity != null) {
                    responseStr = entity;
                }
            } else {<commit_msg>fixed post requests<commit_after>package com.localhop.network;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.SocketTimeoutException;
import java.util.List;

import android.app.Activity;
import android.widget.Toast;
import org.apache.http.*;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.HttpHostConnectException;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

/**
 * Task that connects to a server running HERMIT-web and simulates HERMIT commands
 * by using HTTP GET and POST requests.
 */
public abstract class HttpServerRequest<A extends Activity, Result> extends AsyncActivityTask<A, String, Void, Result> {
    public static String DUMMY_URL = "http://ip.jsontest.com/";
    private HttpRequest mRequest;
    private String mErrorMessage;
    private List<NameValuePair> mData;

    /**
     * Constructs a new instance. The constructor is not the place to put any input
     * {@link JSONObject}s (do that in {@link android.os.AsyncTask#execute(JSONObject...)
     * execute(JSONObject...)} instead).
     *
     * @param console reference to the current console.
     */
    public HttpServerRequest(A activity, HttpRequest request, List<NameValuePair> data) {
        super(activity);
        mRequest = request;
        mData = data;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Result doInBackground(String... params) {
        HttpClient httpClient = null;
        HttpResponse httpResponse = null;
        String responseStr = null;
        final String url = params[0];

        try {
            final HttpParams httpParams = new BasicHttpParams();
            //Set timeout length to 10 seconds
            HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
            HttpConnectionParams.setSoTimeout(httpParams, 10000);
            httpClient = new DefaultHttpClient(httpParams);
            HttpUriRequest request = null;
            if (mRequest.equals(HttpRequest.GET)) {
                request = new HttpGet(url);
            } else if (mRequest.equals(HttpRequest.POST)) {
                final HttpPost httpPost = new HttpPost(url);
                    if (mData != null) {
                        try {
                            httpPost.setEntity(new UrlEncodedFormEntity(mData, "UTF-8"));
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                    }
                request = httpPost;
            }

            if (!isCancelled()) {
                httpResponse = httpClient.execute(request);
            }

            if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                final String entity = EntityUtils.toString(httpResponse.getEntity(), HTTP.UTF_8).trim();
                if (entity != null) {
                    responseStr = entity;
                }
            } else {
{'fraction_non_alphanumeric': 0.09642709339219202, 'fraction_numerical': 0.0046903021106359495, 'mean_word_length': 2.4722222222222223, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1683734', 'n_tokens_mistral': 1915, 'n_tokens_neox': 1844, 'n_words': 445}