<commit_before>/*
 * Copyright 2015 OpenMarket Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.matrix.androidsdk.rest.client;

import android.os.AsyncTask;
import android.util.Log;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * UrlPostTask triggers a POST with no param.
 */
public class UrlPostTask extends AsyncTask<String, Void, String> {

    public interface IPostTaskListener {
        /**
         * The post succceeds.
         * @param object the object retrieves in the response.
         */
        void onSucceed(JsonObject object);

        /**
         * The post failed
         * @param errorMessage the error message.
         */
        void onError(String errorMessage);
    }

    private static final String LOG_TAG = "UrlPostTask";

    // the post listener
    private IPostTaskListener mListener;

    @Override
    protected String doInBackground(String... params) {
        String result = "";

        try {
            // Create a new HttpClient and Post Header
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(params[0]);
            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);

            HttpEntity entity = response.getEntity();

            if (entity != null) {
                // A Simple JSON Response Read
                InputStream instream = entity.getContent();
                result = convertStreamToString(instream);
            }
        } catch(Exception e) {
            // Do something about exceptions
            result = e.getMessage();
        }
        return  result;
    }

    /**
     * Set the post listener
     * @param listener the listener
     */
    public void setListener(IPostTaskListener listener) {
        mListener = listener;
    }

    /**<commit_msg>Use HttpURLConnection instead of HttpPost to be compliant with target sdk > 21<commit_after>/*
 * Copyright 2016 OpenMarket Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.matrix.androidsdk.rest.client;

import android.os.AsyncTask;
import android.util.Log;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * UrlPostTask triggers a POST with no param.
 */
public class UrlPostTask extends AsyncTask<String, Void, String> {

    public interface IPostTaskListener {
        /**
         * The post succceeds.
         *
         * @param object the object retrieves in the response.
         */
        void onSucceed(JsonObject object);

        /**
         * The post failed
         * @param errorMessage
         */
        void onError(String errorMessage);
    }

    private static final String LOG_TAG = "UrlPostTask";

    // the post listener
    private IPostTaskListener mListener;

    @Override
    protected String doInBackground(String... params) {
        String result = "";

        try {
            URL url = new URL(params[0]);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");

            InputStream is = new BufferedInputStream(conn.getInputStream());

            if (is != null) {
                result = convertStreamToString(is);
            }
        } catch (Exception e) {
            // Do something about exceptions
            result = e.getMessage();
        }
        return result;
    }

    /**
     * Set the post listener
     * @param listener the listener
     */
    public void setListener(IPostTaskListener listener) {
        mListener = listener;
    }

    /**
{'fraction_non_alphanumeric': 0.08450155763239875, 'fraction_numerical': 0.003894080996884735, 'mean_word_length': 2.4969366916269573, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7489923', 'n_tokens_mistral': 1346, 'n_tokens_neox': 1300, 'n_words': 451}