curl http://devnexus.com/s/speakers.json | json speakerList.speaker
| mongoimport --type json --jsonArray -c speakers
package net.saga.devnexus.devnexusdemo;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Hello world!
*
*/
public class App {
private static final Charset UTF_8 = Charset.forName("UTF-8");
public static void main( String[] args ) throws MalformedURLException, IOException, JSONException {
URL devNexusURL = null;
HttpURLConnection devNexusConnection = null;
InputStream devNexusInputStream = null;
ByteArrayOutputStream byteStream = new ByteArrayOutputStream(1024);
byte[] byteArray = new byte[1024];
int length = -1;
devNexusURL = new URL("http://devnexus.com/s/speakers.json");
devNexusConnection = (HttpURLConnection) devNexusURL.openConnection();
devNexusInputStream = devNexusConnection.getInputStream();
while((length = devNexusInputStream.read(byteArray)) != -1) {
byteStream.write(byteArray, 0, length);
}
String httpResult = new String(byteStream.toByteArray(), UTF_8);
JSONObject jsonResult = new JSONObject(httpResult);
JSONArray speakers = jsonResult.getJSONObject("speakerList").getJSONArray("speaker");
Mongo mongoClient = new Mongo();
DB db = mongoClient.getDB( "test" );
DBCollection speakerCollection = db.getCollection("speaker");
for (int index = 0; index < speakers.length(); index ++ ) {
JSONObject speaker = speakers.getJSONObject(index);
BasicDBObject speakerDoc = new BasicDBObject();
Iterator keys = speaker.keys();
while (keys.hasNext()) {
String key = keys.next();
speakerDoc.append(key, speaker.getString(key));
}
speakerCollection.insert(speakerDoc);
}
assert speakerCollection.getCount() == speakers.length();
}
}
public class GCMIntentService extends GCMBaseIntentService {
public GCMIntentService() {
super("APP_ID");
}
@Override
protected void onError(Context context, String error) {
Log.e("GCM", error);
}
@Override
protected void onMessage(Context context, Intent message) {
//handle message
}
@SuppressWarnings("unchecked")
@Override
protected void onRegistered(Context context, String regId) {
ServerUtilities.register(context, regId);
}
@Override
protected void onUnregistered(Context context, String regId) {
if (GCMRegistrar.isRegisteredOnServer(context)) {
ServerUtilities.unregister(context, regId);
}
}
}
<permission
android:name="net.saga.android.coffeeexample.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission
android:name="net.saga.android.coffeeexample.permission.C2D_MESSAGE" />
<!-- App receives GCM messages. -->
<uses-permission
android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- GCM connects to Google Services. -->
<uses-permission
android:name="android.permission.INTERNET" />
<!-- GCM requires a Google account. -->
<uses-permission
android:name="android.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission
android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action
android:name="com.google.android.c2dm.intent.RECEIVE" />
<action
android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category
android:name="net.saga.android.coffeeexample" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
Sender sender = new Sender("SERVER_KEY");
Message message = new Message.Builder().build();
MulticastResult result = sender.send(message, regIds, 5);