diff --git a/QuantcastAndroidSdk/AndroidManifest.xml b/QuantcastAndroidSdk/AndroidManifest.xml index 39d4720..ee28e9f 100644 --- a/QuantcastAndroidSdk/AndroidManifest.xml +++ b/QuantcastAndroidSdk/AndroidManifest.xml @@ -6,7 +6,6 @@ - diff --git a/QuantcastAndroidSdk/src/com/quantcast/measurement/service/QCDataUploader.java b/QuantcastAndroidSdk/src/com/quantcast/measurement/service/QCDataUploader.java index 6cf03e8..c18a799 100644 --- a/QuantcastAndroidSdk/src/com/quantcast/measurement/service/QCDataUploader.java +++ b/QuantcastAndroidSdk/src/com/quantcast/measurement/service/QCDataUploader.java @@ -12,21 +12,17 @@ package com.quantcast.measurement.service; -import org.apache.http.HttpResponse; -import org.apache.http.HttpStatus; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.DefaultHttpClient; -import org.apache.http.params.BasicHttpParams; -import org.apache.http.params.CoreProtocolPNames; -import org.apache.http.params.HttpParams; -import org.apache.http.protocol.BasicHttpContext; -import org.apache.http.protocol.HTTP; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import java.io.BufferedWriter; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.net.HttpURLConnection; +import java.net.URL; import java.net.UnknownHostException; +import java.nio.charset.StandardCharsets; import java.util.Collection; class QCDataUploader { @@ -66,25 +62,24 @@ String synchronousUploadEvents(Collection events) { } int code; - String url = QCUtility.addScheme(UPLOAD_URL_WITHOUT_SCHEME); - final DefaultHttpClient defaultHttpClient = new DefaultHttpClient(); - defaultHttpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, - System.getProperty("http.agent")); - final BasicHttpContext localContext = new BasicHttpContext(); + HttpURLConnection urlConnection = null; try { - HttpPost post = new HttpPost(url); - post.setHeader("Content-Type", "application/json"); - StringEntity se = new StringEntity(upload.toString(), HTTP.UTF_8); - post.setEntity(se); - - HttpParams params = new BasicHttpParams(); - params.setBooleanParameter("http.protocol.expect-continue", false); - post.setParams(params); - - HttpResponse response = defaultHttpClient.execute(post, localContext); - code = response.getStatusLine().getStatusCode(); - }catch (UnknownHostException uhe) { + URL url = new URL(QCUtility.addScheme(UPLOAD_URL_WITHOUT_SCHEME)); + urlConnection = (HttpURLConnection) url.openConnection(); + urlConnection.setRequestProperty("User-Agent", System.getProperty("http.agent")); + urlConnection.setDoOutput(true); + urlConnection.setChunkedStreamingMode(0); + + OutputStream os = urlConnection.getOutputStream(); + BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8)); + writer.write(upload.toString()); + writer.flush(); + writer.close(); + os.close(); + + code = urlConnection.getResponseCode(); + } catch (UnknownHostException uhe) { QCLog.e(TAG, "Not connected to Internet", uhe); //don't send this error because its ok if they don't have internet connection return null; @@ -92,6 +87,10 @@ String synchronousUploadEvents(Collection events) { QCLog.e(TAG, "Could not upload events", e); QCMeasurement.INSTANCE.logSDKError("json-upload-failure", e.toString(), null); return null; + } finally { + if (urlConnection != null) { + urlConnection.disconnect(); + } } if (!isSuccessful(code)) { diff --git a/QuantcastAndroidSdk/src/com/quantcast/measurement/service/QCPolicy.java b/QuantcastAndroidSdk/src/com/quantcast/measurement/service/QCPolicy.java index eaf9cd0..9407de3 100644 --- a/QuantcastAndroidSdk/src/com/quantcast/measurement/service/QCPolicy.java +++ b/QuantcastAndroidSdk/src/com/quantcast/measurement/service/QCPolicy.java @@ -15,10 +15,6 @@ import android.net.Uri; import android.telephony.TelephonyManager; -import org.apache.http.HttpResponse; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.impl.client.DefaultHttpClient; -import org.apache.http.params.CoreProtocolPNames; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -30,6 +26,8 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; import java.util.HashSet; import java.util.Locale; import java.util.Set; @@ -150,19 +148,23 @@ private void getPolicy(Context context) { QCLog.i(TAG, "checking load policy: " + loadedPolicy); if (!loadedPolicy) { String jsonString = null; - DefaultHttpClient defaultHttpClient = new DefaultHttpClient(); - defaultHttpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, - System.getProperty("http.agent")); + InputStream inputStream = null; + HttpURLConnection urlConnection = null; try { - HttpGet method = new HttpGet(m_policyURL); - HttpResponse response = defaultHttpClient.execute(method); - inputStream = response.getEntity().getContent(); + URL url = new URL(m_policyURL); + urlConnection = (HttpURLConnection) url.openConnection(); + urlConnection.setRequestProperty("User-Agent", System.getProperty("http.agent")); + + inputStream = urlConnection.getInputStream(); jsonString = readStreamToString(inputStream); } catch (Exception e) { QCLog.e(TAG, "Could not download policy", e); QCMeasurement.INSTANCE.logSDKError("policy-download-failure", e.getMessage(), null); } finally { + if (urlConnection != null) { + urlConnection.disconnect(); + } if (inputStream != null) { try { inputStream.close();