HttpUtilspublic class HttpUtils extends Object
Fields Summary |
---|
private static final String | TAG | private static final boolean | DEBUG | private static final boolean | LOCAL_LOGV | public static final int | HTTP_POST_METHOD | public static final int | HTTP_GET_METHOD | private static final String | HDR_VALUE_ACCEPT_LANGUAGE | private static final String | HDR_KEY_ACCEPT | private static final String | HDR_KEY_ACCEPT_LANGUAGE | private static final String | HDR_KEY_X_WAP_PROFILE | private static final String | HDR_VALUE_ACCEPT |
Constructors Summary |
---|
private HttpUtils()
// To forbidden instantiate this class.
|
Methods Summary |
---|
private static void | addLocaleToHttpAcceptLanguage(java.lang.StringBuilder builder, java.util.Locale locale)
String language = locale.getLanguage();
if (language != null) {
builder.append(language);
String country = locale.getCountry();
if (country != null) {
builder.append("-");
builder.append(country);
}
}
| private static android.net.http.AndroidHttpClient | createHttpClient()
AndroidHttpClient client
= AndroidHttpClient.newInstance("Android-Mms/0.1");
HttpParams params = client.getParams();
HttpProtocolParams.setContentCharset(params, "UTF-8");
return client;
| private static java.lang.String | getHttpAcceptLanguage()Return the Accept-Language header. Use the current locale plus
US if we are in a different locale than US.
Locale locale = Locale.getDefault();
StringBuilder builder = new StringBuilder();
addLocaleToHttpAcceptLanguage(builder, locale);
if (!locale.equals(Locale.US)) {
if (builder.length() > 0) {
builder.append(", ");
}
addLocaleToHttpAcceptLanguage(builder, Locale.US);
}
return builder.toString();
| private static java.lang.String | getXWapProfileHeaderValue(android.content.Context context)
return Settings.Gservices.getString(
context.getContentResolver(),
Settings.Gservices.MMS_X_WAP_PROFILE_URL);
| private static void | handleHttpConnectionException(java.lang.Exception exception)
// Inner exception should be logged to make life easier.
Log.e(TAG, exception.getMessage());
throw new IOException(exception.getMessage());
| protected static byte[] | httpConnection(android.content.Context context, long token, java.lang.String url, byte[] pdu, int method, boolean isProxySet, java.lang.String proxyHost, int proxyPort)A helper method to send or retrieve data through HTTP protocol.
if (url == null) {
throw new IllegalArgumentException("URL must not be null.");
}
if (LOCAL_LOGV) {
Log.v(TAG, "httpConnection: params list");
Log.v(TAG, "\ttoken\t\t= " + token);
Log.v(TAG, "\turl\t\t= " + url);
Log.v(TAG, "\tmethod\t\t= "
+ ((method == HTTP_POST_METHOD) ? "POST"
: ((method == HTTP_GET_METHOD) ? "GET" : "UNKNOWN")));
Log.v(TAG, "\tisProxySet\t= " + isProxySet);
Log.v(TAG, "\tproxyHost\t= " + proxyHost);
Log.v(TAG, "\tproxyPort\t= " + proxyPort);
// TODO Print out binary data more readable.
//Log.v(TAG, "\tpdu\t\t= " + Arrays.toString(pdu));
}
AndroidHttpClient client = null;
try {
// Make sure to use a proxy which supports CONNECT.
URI hostUrl = new URI(url);
HttpHost target = new HttpHost(
hostUrl.getHost(), hostUrl.getPort(),
HttpHost.DEFAULT_SCHEME_NAME);
client = createHttpClient();
HttpRequest req = null;
switch(method) {
case HTTP_POST_METHOD:
ProgressCallbackEntity entity = new ProgressCallbackEntity(
context, token, pdu);
// Set request content type.
entity.setContentType("application/vnd.wap.mms-message");
HttpPost post = new HttpPost(url);
post.setEntity(entity);
req = post;
break;
case HTTP_GET_METHOD:
req = new HttpGet(url);
break;
default:
Log.e(TAG, "Unknown HTTP method: " + method
+ ". Must be one of POST[" + HTTP_POST_METHOD
+ "] or GET[" + HTTP_GET_METHOD + "].");
return null;
}
// Set route parameters for the request.
HttpParams params = client.getParams();
if (isProxySet) {
ConnRouteParams.setDefaultProxy(
params, new HttpHost(proxyHost, proxyPort));
}
req.setParams(params);
// Set necessary HTTP headers for MMS transmission.
req.addHeader(HDR_KEY_ACCEPT, HDR_VALUE_ACCEPT);
{
String xWapProfileUrl = getXWapProfileHeaderValue(context);
if (xWapProfileUrl != null) {
req.addHeader(HDR_KEY_X_WAP_PROFILE, xWapProfileUrl);
}
}
req.addHeader(HDR_KEY_ACCEPT_LANGUAGE, HDR_VALUE_ACCEPT_LANGUAGE);
HttpResponse response = client.execute(target, req);
StatusLine status = response.getStatusLine();
if (status.getStatusCode() != 200) { // HTTP 200 is success.
throw new IOException("HTTP error: " + status.getReasonPhrase());
}
HttpEntity entity = response.getEntity();
byte[] body = null;
if (entity != null) {
try {
if (entity.getContentLength() > 0) {
body = new byte[(int) entity.getContentLength()];
DataInputStream dis = new DataInputStream(entity.getContent());
try {
dis.readFully(body);
} finally {
try {
dis.close();
} catch (IOException e) {
Log.e(TAG, "Error closing input stream: " + e.getMessage());
}
}
}
} finally {
if (entity != null) {
entity.consumeContent();
}
}
}
return body;
} catch (URISyntaxException e) {
handleHttpConnectionException(e);
} catch (IllegalStateException e) {
handleHttpConnectionException(e);
} catch (IllegalArgumentException e) {
handleHttpConnectionException(e);
} catch (SocketException e) {
handleHttpConnectionException(e);
} catch (Exception e) {
handleHttpConnectionException(e);
}
finally {
if (client != null) {
client.close();
}
}
return null;
|
|