MOBILE

Windows Phone 7 : In the Cloud - Creating a Google API–Based Translator

7/20/2012 11:35:42 AM

1. Problem

You want to create an application that interacts with a Representational State Transfer (RESTful) service that returns a response in JavaScript Object Notation (JSON) format.

2. Solution

You must use WebClient and DataContractJsonSerializer.

3. How It Works

WebClient provides common methods for sending and receiving data to and from a resource identified by a URI. In this case, the resource will give you a response in JSON format. Then you will use DataContractJsonSerializer to deserialize the response inside your object graph.

4. The Code

For the purposes of this exercise, you will use an API made available by Google for the translation of texts. (To use this API, you must have a key to access the service itself).

The application that you will create in terms of the UI is quite simple. It will have a text box for entering text to be translated, a button to start the interaction with the service, and a text block to show the result of your request (which in this case will be a translation from English to Italian).

Here is the XAML that composes our interface:

<Grid X:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
  <Grid.RowDefinitions>
    <RowDefinition Height="*" />
    <RowDefinition Height="Auto" />
    <RowDefinition Height="*" />
  </ Grid.RowDefinitions>
  <TextBox Name="TextToTranslateTextbox" Text="Translate Me" />
  <Button Name = "TranslateButton" Content = "Translate" Grid.Row = "1"
                    Click = "TranslateButton_Click" />
  <TextBlock Name="TranslatedTextblock" Grid.Row="2" />
</ Grid>

Then in the code-behind of your PhoneApplicationPage, add class-level member as follows:

WebClient proxy = null;

The WebClient class provides common methods for sending and receiving data from a resource identified by an URI, so it's perfect for our purpose:

private WebClient proxy = null;

And now in the constructor of your page you need to instantiate the proxy and prepare it to manage the response of an OpenRead request,

proxy = new WebClient ();
proxy.OpenReadCompleted + = new OpenReadCompletedEventHandler (proxy_OpenReadCompleted);

					  

In the event handler, you will insert code to handle the response, but let's focus first on the request, which will be linked to the click of TranslateButton:

...
private void TranslateButton_Click (object sender, RoutedEventArgs e)
{
  try
  {
    if (String.IsNullOrEmpty (TextToTranslateTextbox.Text))
    {
      Uri request = buildGoogleTranslateRequest (TextToTranslateTextbox.Text);
      proxy.OpenReadAsync (request);
    }
  }
  catch (Exception ex)
  {
    // Do something with this exception
  }
}

...

					  

And buildGoogleTranslateRequest(string) is our private method that creates the URI for your request:

private Uri buildGoogleTranslateRequest(string textToTranslate)
{
        return new Uri("https://www.googleapis.com/language/translate/v2?
            key=YOUR_API_KEY_HERE&
            q=" + textToTranslate + "
          &source=en
          &target=it
          &prettyprint=true");
}

As a result of this request, the application will give you an object serialized in JSON format—more precisely, the format is as follows:

{
    "Data": {
        "Translations": [
            {
                "TranslatedText": "Text"
            }
        ]
    }
}

That tells you that the root object of the object graph has an internal object of type Date, which in turn contains an array of objects of type Translations, which in turn contains a property of type string called TranslatedText. Figure 1 shows the class diagram.

Figure 1. Class diagram of classes used to deserialize the objects

The implementation of GoogleTranslation is as follows:

GoogleTranslation.cs

[DataContract]
    public class GoogleTranslation
    {
        [DataMember (Name = "data")]
        public Data Data {get; set;}
    }

Data.cs

[DataContract]
    public class Data
    {
        [DataMember (Name = "translations")]
        public List <Translation> Translations {get, set;}
    }

Translation.cs

[DataContract]
    public class Translation
    {
        [DataMember (Name = "translatedText")]
        public string TranslatedText {get, set;}
    }
...

The annotations on classes says you which type of serializer you will use —that is, DataContractJsonSerializer, which will be the focus of the implementation dell'eventHandler proxy_OpenReadCompleted.Il DataContractJsonSerializer System.ServiceModel.Web is contained in the assembly (so remember to add this reference to your project).

void proxy_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
  var serializer = new DataContractJsonSerializer(typeof(GoogleTranslation));
  GoogleTranslation translation = serializer.ReadObject(e.Result) as GoogleTranslation;

  this.TranslatedTextblock.Text = translation.Data.Translations.FirstOrDefault() != null
  ? translation.Data.Translations.FirstOrDefault().TranslatedText
  : "error in translation";
}

					  

As you can see, the interaction with a REST service is very simple, as is the use of JSON instead of XML. The only difficulty you may encounter is the interpretation of the object graph, to create a model for the deserialization of the response (or the serialization of a request).

5. Usage

Open Visual Studio and start deploying the application to the emulator. Write a sentence in the text box and look for the translation. Of course, you need to stay connected to the Internet to use this recipe.

Other  
  •  Is Small Beautiful, Or Is Bigger Better For Ios Devices?
  •  Filemaker Go 12 For Ipad
  •  Chrome FOR Android Beta Is Out
  •  Apple’s Sandboxing Security Issue
  •  XNA Game Studio 4.0 Programming : The Many Keys Of A Keyboard (part 2) - Moving Sprite Based on Keyboard Input, Onscreen Keyboard
  •  XNA Game Studio 4.0 Programming : The Many Keys Of A Keyboard (part 1) - Reading Keyboard State
  •  Personalize Your iPhone Case
  •  iOS 6's release
  •  Cheap smartphones at Computex 2012 : Acer CloudMobile S500, Gigabyte GSmart G1362, Malata Z500
  •  5 MP3 players in 2012
  •  Blackberry World 2012 (Part 3) - Mobile computing platform
  •  Blackberry World 2012 (Part 2) - BlackBerry 10, Apps and development
  •  Blackberry World 2012 (Part 1) - The keynote address
  •  World's Most Popular IM Client Just Got Hotter
  •  V For Venerable One
  •  The Human Touch
  •  Some Cool Apps From Various Flatforms To Make Your Life Easy
  •  A Bite of Apple iOS 6
  •  “TU ME” …vs Skype and Whatsapp.
  •  Pandora On Android-Your Best Music Buddy!
  •  
    Video
    Top 10
    Nikon 1 J2 With Stylish Design And Dependable Image And Video Quality
    Canon Powershot D20 - Super-Durable Waterproof Camera
    Fujifilm Finepix F800EXR – Another Excellent EXR
    Sony NEX-6 – The Best Compact Camera
    Teufel Cubycon 2 – An Excellent All-In-One For Films
    Dell S2740L - A Beautifully Crafted 27-inch IPS Monitor
    Philips 55PFL6007T With Fantastic Picture Quality
    Philips Gioco 278G4 – An Excellent 27-inch Screen
    Sony VPL-HW50ES – Sony’s Best Home Cinema Projector
    Windows Vista : Installing and Running Applications - Launching Applications
    Most View
    Windows 7 : How to Troubleshoot Driver Problems
    Windows Vista : Programming the Windows Script Host - Programming Objects
    Hack Your Phone (Part 3)
    Sync Your iPad with iTunes : Manually Transferring Music, Movies, Podcasts, and More on Your iPad (Drag-and-Drop Method)
    Mind Control (Part 2) - Home entertainment & gaming
    Adobe's Creative Suite Dreams
    Alphacool NexXxos XT60 Full Copper 240mm
    Windows 7 : Protecting Your Data from Loss and Theft - Disk Organization for Data Safety, BitLocker Disk Encryption
    Windows Server 2003 : Troubleshooting Group Policy
    Macro Lenses - What’s Available?
    Something You Should Know About Iphone 5 (Part 2)
    Multifunction Printer Group Test (Part 2) : Epson Stylus Photo PX730WD, HP Photosmart 5520 e-ALL-in-ONE
    Keep Kids Online Safely (Part 3)
    Programming the iPhone : Progressive Enhancement - Location Awareness
    Beginning Android 3 : Working with Containers - Tabula Rasa
    Business Intelligence in SharePoint 2010 with Business Connectivity Services : External Content Types (part 1)
    Nikon unveiled the 24-85mm lens designed for format FX and 18-300mm for DX
    Understanding Exchange Policy Enforcement Security : Implementing Transport Agent Policies on the Edge
    New Year Gift Guide 2013 (Part 4)
    Introduction to Xcode Simulator