How to read URDU from url and show it to your mobile using java Me

Posted by Basit on Stack Overflow See other posts from Stack Overflow or by Basit
Published on 2010-05-31T10:46:46Z Indexed on 2010/05/31 10:53 UTC
Read the original article Hit count: 158

Filed under:

HI, Hope you all will be fine. Actually I m facing a problem. Actually i am using Google translation API. What my application does it connect to CGI-script, i pass value to it using GET then the CGI script connect to Google API, Translate the mesage from english to Urdu and then i retreive it.Here is the code

[Java]
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

/**
 * An example MIDlet to invoke a CGI script (GET method).
 */

public class InvokeCgiMidlet1 extends MIDlet {

   private Display display;


   String url = "http://xxx.xxx.xx.xxx/cgi-bin/api/GT/GT_Send_Msg.cgi?message=my%20name%20is%20basit";

   public InvokeCgiMidlet1() {
      display = Display.getDisplay(this);
   }

   /**
    * Initialization. Invoked when we activate the MIDlet.
    */
   public void startApp() {
      try {
         getGrade(url);
      } catch (IOException e) {
         System.out.println("IOException " + e);
         e.printStackTrace();
      }
   }

   /**
    * Pause, discontinue ....
    */
   public void pauseApp() {
   }

   /**
    * Destroy must cleanup everything.
    */
   public void destroyApp(boolean unconditional) {
   }

   /**
    * Retrieve a grade....
    */

   void getGrade(String url) throws IOException {
      HttpConnection c = null;
      InputStream is = null;
      OutputStream os = null;
      StringBuffer b = new StringBuffer();
      TextBox t = null;
      String response;
      try {
          c = (HttpConnection)Connector.open(url, Connector.READ_WRITE);
          c.setRequestMethod(HttpConnection.GET);
          c.setRequestProperty("User-Agent","Profile/MIDP-1.0 Confirguration/CLDC-1.0");
          c.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
          c.setRequestProperty("User-Agent","Profile/MIDP-2.0 Configuration/CLDC-1.1");
          c.setRequestProperty("Accept-Charset","UTF-8;q=0.7,*;q=0.7");
          c.setRequestProperty("Accept-Encoding","gzip, deflate");
          c.setRequestProperty("Accept","text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
          c.setRequestProperty("Content-Language", "en-EN");
          os = c.openOutputStream();

        Reader r = new InputStreamReader(c.openDataInputStream(), "UTF-8");

         int ch;
         while ((ch = r.read()) != -1) {
            b.append((char) ch );
            //System.out.println((char)ch + "->" + ch + "->" + ch);

         }

         t = new TextBox("Final Grades", b.reverse().toString(), 1024, 0);
      } finally {
         if(is!= null) {
            is.close();
         }
         if(os != null) {
            os.close();
         }
         if(c != null) {
            c.close();
         }
      }
      display.setCurrent(t);
   }
}
[/Java] 

The problem is as i told you that the translated text is in Urdu. So when it appear on screen each character is separate like this.

? ?? ? ? ?? .

Because i read character by character

I want it to appear in proper form like this

???????

So how can i do this. Is there font rendering required. If yes then how can i do it or any other method please hep me.

Thanks

© Stack Overflow or respective owner

Related posts about java-me