Skip to content

Commit

Permalink
Add support for custom HTTP headers
Browse files Browse the repository at this point in the history
  • Loading branch information
egoetschalckx committed Jan 14, 2014
1 parent 2d9f62a commit 52a75bc
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;

import org.json.JSONObject;

Expand Down Expand Up @@ -92,6 +93,10 @@ public void onError(Exception ex) {
};

ParallelHttpClient httpClient = new ParallelHttpClient();
for (Map.Entry<String, String> entry : mConnection.getHeaders().entrySet())
{
httpClient.addHeader(entry.getKey(), entry.getValue());
}
ParameterMap params = httpClient.newParams()
.add("data", text.toString());
httpClient.setMaxRetries(1);
Expand Down Expand Up @@ -181,6 +186,10 @@ public void onError(Exception ex) {
httpClient.setMaxRetries(1);
httpClient.setConnectionTimeout(5000);
httpClient.setReadTimeout(115000);
for (Map.Entry<String, String> entry : mConnection.getHeaders().entrySet())
{
httpClient.addHeader(entry.getKey(), entry.getValue());
}
ParameterMap params = httpClient.newParams();
httpClient.post(url, params, cb);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import com.zsoft.SignalA.Transport.TransportHelper;
import com.zsoft.parallelhttpclient.ParallelHttpClient;

import java.util.Map;

public class ConnectingState extends StopableStateWithCallback {
public ConnectingState(ConnectionBase connection) {
super(connection);
Expand Down Expand Up @@ -100,6 +102,10 @@ public void onError(Exception ex) {

ParallelHttpClient httpClient = new ParallelHttpClient();
httpClient.setMaxRetries(1);
for (Map.Entry<String, String> entry : mConnection.getHeaders().entrySet())
{
httpClient.addHeader(entry.getKey(), entry.getValue());
}
httpClient.get(url, null, cb);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;

import android.util.Log;

Expand Down Expand Up @@ -74,6 +75,10 @@ public void onError(Exception ex) {

ParallelHttpClient httpClient = new ParallelHttpClient();
httpClient.setMaxRetries(1);
for (Map.Entry<String, String> entry : mConnection.getHeaders().entrySet())
{
httpClient.addHeader(entry.getKey(), entry.getValue());
}
ParameterMap params = httpClient.newParams();
httpClient.post(url, params, cb);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import com.zsoft.SignalA.Transport.TransportHelper;
import com.zsoft.parallelhttpclient.ParallelHttpClient;

import java.util.Map;

public class ReconnectingState extends StopableStateWithCallback {

public ReconnectingState(ConnectionBase connection) {
Expand All @@ -36,7 +38,7 @@ public void Send(CharSequence text, SendCallback callback) {

@Override
protected void OnRun() {
if(DoStop()) return;
if(DoStop()) return;

if (mConnection.getMessageId() == null)
{
Expand Down Expand Up @@ -121,6 +123,10 @@ public void onError(Exception ex) {

ParallelHttpClient httpClient = new ParallelHttpClient();
httpClient.setMaxRetries(1);
for (Map.Entry<String, String> entry : mConnection.getHeaders().entrySet())
{
httpClient.addHeader(entry.getKey(), entry.getValue());
}
httpClient.post(url, null, cb);
}

Expand Down
13 changes: 12 additions & 1 deletion SignalA/src/main/java/com/zsoft/signala/ConnectionBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

import android.content.Context;

import java.util.Map;
import java.util.TreeMap;

public abstract class ConnectionBase {
private Object mStateLock = new Object();
private StateBase mCurrentState = null;
Expand All @@ -18,6 +21,7 @@ public abstract class ConnectionBase {
private ITransport mTransport;
private String mGroupsToken = null;
private String mQueryString = null;
private Map<String, String> mHeaders = new TreeMap<String, String>();

public ConnectionBase(String url, Context context, ITransport transport, String queryString)
{
Expand Down Expand Up @@ -127,6 +131,13 @@ public void setError(Exception exception)
{
OnError(exception);
}

public void addHeader(String header, String value)
{
mHeaders.put(header, value);
}

public Map<String, String> getHeaders() { return mHeaders; }

// Methods for the user to implement

Expand All @@ -152,5 +163,5 @@ public void Send(CharSequence text, SendCallback callback) {




}

0 comments on commit 52a75bc

Please sign in to comment.