Developing SevenDrops - A Dropbox-based photo uploader for Windows Phone 7 [3/6]

DZone 's Guide to

Developing SevenDrops - A Dropbox-based photo uploader for Windows Phone 7 [3/6]

· Mobile Zone ·
Free Resource

So now there is the JSON-formatted data that represents the unique token and secret. It is time to extract it and store locally, so that the user won't have to call the same method over and over again to obtain this data (notice that this is also not recommended by the Dropbox team).

First of all, you need a reference to System.Json.dll. By default, you can't select it from the list of available Windows Phone 7 libraries, but you can easily find it here: C:\Program Files\Microsoft SDKs\Silverlight\v4.0\Libraries\Client.

Now you can actually parse the token out of the JSON response. To do this, I will modify the HandleResult method a bit so now it looks like this:

private void HandleResult(WebResponse response)
HttpWebResponse _response = (HttpWebResponse)response;

using (StreamReader reader = new StreamReader(response.GetResponseStream()))
string a = reader.ReadToEnd();
JsonValue jValue = JsonObject.Parse(a);

Here I am directly getting the token from the response by parsing the JSON. But there is one small issue - what if the user won't enter the correct credentials? Right now, a scenario like this will result in an unhandled exception that will be this:

Not exactly something you want. And since I am using Reactive Extensions, the regular try/catch approach won't work here since the exception that is generated in this case is deep inside BeginInvoke when the asynchronous operation is started. To handle it, we have to actually check the result of the asynchronous operation when we are subscribing to it.

Let's go back to the event handler for the Login button and modify the subscription method:

n.Subscribe(t => { HandleResult(t); }, ex => { Dispatcher.BeginInvoke(new Action(() => MessageBox.Show("There was an error getting the token."))); });

The first result - t, represents the scenario when there is an actual WebResponse instance present. If that's the case, all I have to do is pass that instance to HandleResult and that will handle the token. The second possible result is ex, an exception that is thrown when processing the asynchronous request.

To avoid an invalid cross-thread operation, I am using Dispatcher.BeginInvoke (remember that MessageBox works on the UI thread). In case of an exception, I will simply show a message box that will tell the user that there was a problem with the token aquisition.

Now since there will be a token when the method passes, I need to actually store it. The best way to do it right now is to simply keep both the token and the secret internally in the isolated storage.

IsolatedStorageSettings.ApplicationSettings["token"] = (string)jValue["token"];

IsolatedStorageSettings.ApplicationSettings["secret"] = (string)jValue["secret"];


Since JSON objects cannot be directly serialized, I cannot simply pass the object and expect it to be automatically converted, so I simply transform the values to raw strings.

Now that you stored the token and the secret, you are able to perform other data manipulations. Specifically, upload files to Dropbox, and that will be discussed in the next article.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}