""

Tag Archive: google

Why flush ?

Interesting stuff i came to know about which i was unaware of. This post is more of a discussion on the problem. I haven’t understood the solution and so would request you to have a say.

I have done json parsing numerous times. I can tell if json is valid or not by looking it. I was using google-gson for android application and its awesome. Application was supported by php web service. The solution that fixed the problem is related to php and i am not a php developer and don’t have that much inner understanding of it. It was regular json parsing routine. Application just crashed when gson was parsing the json response from php service.

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 2

From error I understood the reason but that was not it. I was very confident that something else was the problem because json response was perfect. I wrote quick objective-c routine(SBJSON) to parse same json response and it worked there. I even did validation on jsonlint.com
After doing bit of research I came across a working solution. In php before sending response I had to put this:

ob_clean();
echo header('Content-Type: application/json');

I checked the php manual to understand what those lines means, header function was quite obvious. According to manual, ob_clean:

This function discards the contents of the output buffer.

Now I have no idea why discarding the contents of output buffer was working. This was the case for android, I mean in iphone I didn’t need ob_clean. I am still looking for the explanation on this.

Today i was doing some experiment with google translate API. I was trying to use google translate API with AS3. I found many snippets while googling that was using flex but i wanted to do it in flash as3. So I wrote a very basic code snippet to translate text from one language to other using google translate API.

Well i have created two combobox on my fla. I have given instance name to them and then i am just taking the language input for translating text from one language to other.

The main crucial part:

‘http://ajax.googleapis.com/ajax/services/language/translate’+’?v=2.0’+’&q=’+escape(my_input)+’&langpair=’+’es’+’%7C’+’en’

I am just making a GET HTTP Call to google translate API passing input, and language pair (i.e en(english) to es(espanol) etc). It returns result in JSON. On my flash client i decode that JSON and display the translated result.
Note: escape method is used to encode the string to url encoded format

Here is the source code:

package 
{
	import com.adobe.serialization.json.JSON;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import fl.data.DataProvider; 
	
	/**
	 * ...
	 * @author padam
	 */
	public class Main extends Sprite 
	{
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			
			initGUI();
			button_p.addEventListener(MouseEvent.CLICK, translate);
		}
		
		private function initGUI():void {
			var itemsFrom:Array = [ 
			{label:"English", data:"en"}, 
			{label:"Spanish", data:"es"}, 
			{label:"French", data:"fr"}, 
			{label:"Italian", data:"it"}
			];
			
			var itemsTo:Array = [
			{label:"Hindi", data:"hi"},
			{label:"English", data:"en"}, 
			{label:"Spanish", data:"es"}, 
			{label:"French", data:"fr"}, 
			{label:"Italian", data:"it" }
			];
			
			fromLanguageCombo.prompt = "Select Language"; 
			fromLanguageCombo.dataProvider = new DataProvider(itemsFrom);
			toLanguageCombo.dataProvider = new DataProvider(itemsTo);
			
		}
		
		private function translate(event:MouseEvent):void {
			var request:URLRequest=new URLRequest();
			request.url='http://ajax.googleapis.com/ajax/services/language/translate'+'?v=2.0'+'&q='+escape(fromInput.text)+'&langpair='+fromLanguageCombo.selectedItem.data+'%7C'+toLanguageCombo.selectedItem.data;
			request.method = URLRequestMethod.GET;
			
			var loader:URLLoader=new URLLoader();
			loader.addEventListener(Event.COMPLETE,messageSent);
			try 
			{
				loader.load(request);
			} 
			catch (error:Error) 
			{
				
			}
		}
		
		private function messageSent(evt:Event):void
		{
			var loader:URLLoader=URLLoader(evt.target);
			var result:String = loader.data as String;
			
			var json_1:Object = JSON.decode(result);
			output.text = json_1["responseData"]["translatedText"];
			translationLabel.text = fromLanguageCombo.selectedItem.label + " to " + toLanguageCombo.selectedItem.label+" translation";
		
		}
		
	}
	
}

Well there is nothing much in code. Just taking the language values from two combo box list and passing it to the translate api. Then after decoding json that i received from api call, i am displaying on textfield. Anyway please note that translate api has been officialy depracated.

Here is the quote from API site:

Important: The Google Translate API has been officially deprecated as of May 26, 2011. Due to the substantial economic burden caused by extensive abuse, the number of requests you may make per day will be limited and the API will be shut off completely on December 1, 2011. For website translations, we encourage you to use the Google Translate Element.

Gracias.