Created
February 6, 2014 11:07
-
-
Save toms972/8842217 to your computer and use it in GitHub Desktop.
As of Android OS 4.4, HttpURLConnection fails to parse non standard HTTP headers. Here is a workaround based on the Stackoverflow discussion here: http://stackoverflow.com/questions/19759966/how-to-parse-a-none-standard-http-response
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.myapplication.utils.network; | |
import org.apache.http.HttpResponseFactory; | |
import org.apache.http.impl.conn.DefaultClientConnection; | |
import org.apache.http.impl.conn.DefaultResponseParser; | |
import org.apache.http.io.HttpMessageParser; | |
import org.apache.http.io.SessionInputBuffer; | |
import org.apache.http.params.HttpParams; | |
/** | |
* User: Tom | |
* Date: 2/6/14 | |
* Time: 10:10 AM | |
*/ | |
public class IcyClientConnection extends DefaultClientConnection{ | |
@Override | |
protected HttpMessageParser createResponseParser(SessionInputBuffer buffer, HttpResponseFactory responseFactory, HttpParams params) { | |
return new DefaultResponseParser( | |
buffer, | |
new IcyLineParser(), | |
responseFactory, | |
params); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.myapplication.utils.network; | |
import org.apache.http.conn.OperatedClientConnection; | |
import org.apache.http.conn.scheme.SchemeRegistry; | |
import org.apache.http.impl.conn.DefaultClientConnectionOperator; | |
/** | |
* User: tom | |
* Date: 2/6/14 | |
* Time: 10:12 AM | |
* To change this template use File | Settings | File Templates. | |
*/ | |
public class IcyClientConnectionOperator extends DefaultClientConnectionOperator { | |
public IcyClientConnectionOperator(SchemeRegistry schemes) { | |
super(schemes); | |
} | |
@Override | |
public OperatedClientConnection createConnection() { | |
return new IcyClientConnection(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.myapplication.utils.network; | |
import org.apache.http.conn.ClientConnectionOperator; | |
import org.apache.http.conn.scheme.SchemeRegistry; | |
import org.apache.http.impl.conn.SingleClientConnManager; | |
import org.apache.http.params.HttpParams; | |
/** | |
* User: tom | |
* Date: 2/6/14 | |
* Time: 10:14 AM | |
*/ | |
public class IcyClientConnManager extends SingleClientConnManager{ | |
public IcyClientConnManager(HttpParams params, SchemeRegistry schreg) { | |
super(params, schreg); | |
} | |
@Override | |
protected ClientConnectionOperator createConnectionOperator(SchemeRegistry schreg) { | |
return new IcyClientConnecationOperator(schreg); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.myapplication.utils.network; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.client.HttpClient; | |
import org.apache.http.client.methods.HttpGet; | |
import org.apache.http.conn.scheme.PlainSocketFactory; | |
import org.apache.http.conn.scheme.Scheme; | |
import org.apache.http.conn.scheme.SchemeRegistry; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import org.apache.http.params.BasicHttpParams; | |
import org.apache.http.params.HttpParams; | |
import org.apache.http.params.HttpProtocolParams; | |
import java.io.IOException; | |
/** | |
* User: tom | |
* Date: 2/6/14 | |
* Time: 10:26 AM | |
*/ | |
public class IcyGetRequest { | |
private HttpClient _httpClient; | |
private String _url; | |
public IcyGetRequest(String url) { | |
_url = url; | |
Scheme http = new Scheme("http", PlainSocketFactory.getSocketFactory(), 80); | |
SchemeRegistry sr = new SchemeRegistry(); | |
sr.register(http); | |
HttpParams params = new BasicHttpParams(); | |
_httpClient = new DefaultHttpClient(new IcyClientConnManager(params, sr), params); | |
} | |
public HttpResponse get() throws IOException { | |
HttpGet request = new HttpGet(_url); | |
return _httpClient.execute(request); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.myapplication.utils.network; | |
import org.apache.http.Header; | |
import org.apache.http.ParseException; | |
import org.apache.http.ProtocolVersion; | |
import org.apache.http.StatusLine; | |
import org.apache.http.message.BasicLineParser; | |
import org.apache.http.message.ParserCursor; | |
import org.apache.http.util.CharArrayBuffer; | |
/** | |
* User: tom | |
* Date: 2/6/14 | |
* Time: 11:28 AM | |
*/ | |
public class IcyLineParser extends BasicLineParser{ | |
@Override | |
public Header parseHeader(CharArrayBuffer buffer) throws ParseException { | |
return super.parseHeader(this.meltBuffer(buffer)); | |
} | |
@Override | |
public ProtocolVersion parseProtocolVersion(CharArrayBuffer buffer, ParserCursor cursor) throws ParseException { | |
CharArrayBuffer meltedBuffer = meltBuffer(buffer); | |
cursor = meltCursor(meltedBuffer, buffer, cursor); | |
return super.parseProtocolVersion(meltedBuffer, cursor); | |
} | |
@Override | |
public boolean hasProtocolVersion(CharArrayBuffer buffer, ParserCursor cursor) { | |
CharArrayBuffer meltedBuffer = meltBuffer(buffer); | |
cursor = meltCursor(meltedBuffer, buffer, cursor); | |
return super.hasProtocolVersion(meltedBuffer, cursor); | |
} | |
@Override | |
public StatusLine parseStatusLine(CharArrayBuffer buffer, ParserCursor cursor) throws ParseException { | |
CharArrayBuffer meltedBuffer = meltBuffer(buffer); | |
cursor = meltCursor(meltedBuffer, buffer, cursor); | |
return super.parseStatusLine(meltedBuffer, cursor); | |
} | |
private CharArrayBuffer meltBuffer(CharArrayBuffer buffer) { | |
if (buffer.length() >= 4 && | |
buffer.charAt(0) == 'I' && | |
buffer.charAt(1) == 'C' && | |
buffer.charAt(2) == 'Y' && | |
buffer.charAt(3) == ' ' && | |
Character.isDigit(buffer.charAt(4))) | |
{ | |
CharArrayBuffer fixedBuffer = new CharArrayBuffer(buffer.capacity() + 5); | |
fixedBuffer.append("HTTP/1.0 "); | |
fixedBuffer.append(buffer, 4, buffer.length() - 4); | |
buffer = fixedBuffer; | |
} | |
return buffer; | |
} | |
private ParserCursor meltCursor(CharArrayBuffer meltedBuffer, CharArrayBuffer buffer, ParserCursor cursor) { | |
if (meltedBuffer.length != buffer.length) { | |
// Create a new cursor if the new buffer size has changed | |
return new ParserCursor(0, meltedBuffer.length()); | |
} | |
return cursor; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment