Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Apache HTTP Client to HttpURLConnection #19

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion QuantcastAndroidSdk/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

<application
android:usesCleartextTraffic="true">
<uses-library android:name="org.apache.http.legacy" android:required="false"/>

<activity android:name="com.quantcast.measurement.service.AboutQuantcastScreen"
android:label="OtherName"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -66,32 +62,35 @@ String synchronousUploadEvents(Collection<QCEvent> 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;
} catch (Exception e) {
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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down