I'll make some adjustments to the project in the next days:
Add Android Wear compatible notifications- Already the case, as we were using the NotificationCompat classes
- Add a small Android Wear app
If you have any questions, don't hesitate to ask.
private class MySSLSocketFactory extends SSLSocketFactory {
SSLContext sslContext = SSLContext.getInstance("TLS");
public MySSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException,
KeyManagementException, KeyStoreException,
UnrecoverableKeyException {
super(truststore);
TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
sslContext.init(null, new TrustManager[]{tm}, null);
}
@Override
public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException {
return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
}
@Override
public Socket createSocket() throws IOException {
return sslContext.getSocketFactory().createSocket();
}
}
import android.content.Context;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ClientConnectionManager;
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.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.security.KeyStore;
public class MyRestService {
private final static String TAG = MyRestService.class.getSimpleName();
private HttpClient httpClient = null;
private Context context = null;
public MyRestService(Context context) {
this.context = context;
this.httpClient = new MyHttpClient(this.context);
}
protected String post(URI api, Header[] headers, HttpEntity entity) /* ... throws */ {
String responseBody;
try {
HttpPost method = new HttpPost(api);
method.setEntity(entity);
method.setHeaders(headers);
HttpResponse response = httpClient.execute(method);
StatusLine status = response.getStatusLine();
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseBody = out.toString();
if (status.getStatusCode() != HttpStatus.SC_OK) {
if (status.getStatusCode() == HttpStatus.SC_FORBIDDEN) {
//process forbidden status code
} else if (status.getStatusCode() == HttpStatus.SC_REQUEST_TIMEOUT) {
//process request time out code
} else {
//process unrecoverable errors
}
//...
}
} catch (IOException e) {
//Log the exception, e.g. Log.e(TAG, e.getMessage(), e); or Logger.e(TAG, e);
//process the exception
}
return responseBody;
}
protected Context getContext() {
return context;
}
private class MyHttpClient extends DefaultHttpClient {
final Context context;
public MyHttpClient(Context ctx) {
this.context = ctx;
}
@Override
protected ClientConnectionManager createClientConnectionManager() {
SchemeRegistry registry = new SchemeRegistry();
registry.register("http", PlainSocketFactory.getSocketFactory(), 80));
registry.register("https", newSslSocketFactory(), 443));
HttpParams httpParameters = getParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 10000);
HttpConnectionParams.setSoTimeout(httpParameters, 10000);
HttpProtocolParams.setVersion(httpParameters, HttpVersion.HTTP_1_1);
HttpProtocolParams.setUseExpectContinue(httpParameters, false);
return new ThreadSafeClientConnManager(httpParameters, registry);
}
private SSLSocketFactory newSslSocketFactory() {
try {
KeyStore trusted = KeyStore.getInstance("BKS");
InputStream in = this.context.getResources().openRawResource(R.raw.mystore);
try {
trusted.load(in, "MyStorePassword");
} finally {
in.close();
}
return new SSLSocketFactory(trusted);
} catch (Exception e) {
throw new AssertionError(e);
}
}
}
}