Use Apache HTTPClient for EventLogging

Using different HTTP implementations in different parts of the code
is asking for trouble
This commit is contained in:
YuviPanda 2013-03-14 01:13:17 +05:30
parent b9f6c65f45
commit 87b81005ab
2 changed files with 14 additions and 20 deletions

View file

@ -4,25 +4,17 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.StringWriter; import java.io.StringWriter;
import java.net.URI; import java.net.URI;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.xml.transform.*;
import android.accounts.*; import android.accounts.*;
import android.app.Application; import android.app.Application;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build; import android.os.Build;
import com.nostra13.universalimageloader.cache.disc.impl.TotalSizeLimitedDiscCache; import com.nostra13.universalimageloader.cache.disc.impl.TotalSizeLimitedDiscCache;
import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.download.HttpClientImageDownloader;
import com.nostra13.universalimageloader.core.download.ImageDownloader;
import com.nostra13.universalimageloader.core.download.URLConnectionImageDownloader; import com.nostra13.universalimageloader.core.download.URLConnectionImageDownloader;
import com.nostra13.universalimageloader.utils.StorageUtils; import com.nostra13.universalimageloader.utils.StorageUtils;
import org.acra.ACRA; import org.acra.ACRA;
@ -30,11 +22,8 @@ import org.acra.ReportingInteractionMode;
import org.acra.annotation.ReportsCrashes; import org.acra.annotation.ReportsCrashes;
import org.apache.http.client.HttpClient; import org.apache.http.client.HttpClient;
import org.mediawiki.api.*; import org.mediawiki.api.*;
import org.w3c.dom.Node;
import org.wikimedia.commons.auth.WikiAccountAuthenticator; import org.wikimedia.commons.auth.WikiAccountAuthenticator;
import org.apache.http.HttpVersion; import org.apache.http.impl.client.*;
import org.apache.http.conn.ConnectionKeepAliveStrategy;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames; import org.apache.http.params.CoreProtocolPNames;
import org.wikimedia.commons.data.DBOpenHelper; import org.wikimedia.commons.data.DBOpenHelper;
@ -66,10 +55,14 @@ public class CommonsApplication extends Application {
public static final String DEFAULT_EDIT_SUMMARY = "Uploaded using Android Commons app"; public static final String DEFAULT_EDIT_SUMMARY = "Uploaded using Android Commons app";
public static MWApi createMWApi() { public static AbstractHttpClient createHttpClient() {
DefaultHttpClient client = new DefaultHttpClient(); DefaultHttpClient client = new DefaultHttpClient();
client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Commons/" + APPLICATION_VERSION + " (https://mediawiki.org/wiki/Apps/Commons) Android/" + Build.VERSION.RELEASE); client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Commons/" + APPLICATION_VERSION + " (https://mediawiki.org/wiki/Apps/Commons) Android/" + Build.VERSION.RELEASE);
return new MWApi(API_URL, client); return client;
}
public static MWApi createMWApi() {
return new MWApi(API_URL, createHttpClient());
} }

View file

@ -4,6 +4,8 @@ import android.os.AsyncTask;
import android.os.Build; import android.os.Build;
import android.text.TextUtils; import android.text.TextUtils;
import de.akquinet.android.androlog.Log; import de.akquinet.android.androlog.Log;
import in.yuvi.http.fluent.Http;
import org.apache.http.HttpResponse;
import org.apache.http.client.utils.URLEncodedUtils; import org.apache.http.client.utils.URLEncodedUtils;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
@ -20,16 +22,15 @@ public class EventLog {
protected Boolean doInBackground(LogBuilder... logBuilders) { protected Boolean doInBackground(LogBuilder... logBuilders) {
boolean allSuccess = true; boolean allSuccess = true;
// Not using the default URL connection, since that seems to have different behavior than the rest of the code
// Going to simply use the default URLConnection.
// This should be as lightweight as possible, and doesn't really do any fancy stuff
for(LogBuilder logBuilder: logBuilders) { for(LogBuilder logBuilder: logBuilders) {
HttpURLConnection conn; HttpURLConnection conn;
try { try {
URL url = logBuilder.toUrl(); URL url = logBuilder.toUrl();
conn = (HttpURLConnection) url.openConnection(); HttpResponse response = Http.get(url.toString()).use(CommonsApplication.createHttpClient()).asResponse();
int respCode = conn.getResponseCode();
if(respCode != 204) { if(response.getStatusLine().getStatusCode() != 204) {
allSuccess = false; allSuccess = false;
} }
Log.d("Commons", "EventLog hit " + url.toString()); Log.d("Commons", "EventLog hit " + url.toString());