⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 translatorscreen.java

📁 J2ME MIDP_Example_Applications
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        {
            midlet.translatorScreenSettings();
        }
    }
    
    
    private byte[] getTranslationSOAPRequest(String inputLocale,
                                             String outputLocale,
                                             String inputText)
        throws UnsupportedEncodingException
    {
        // You could use an appropriate SOAP helper classes to construct
        // the request message in approximately the following manner:
        //   String methodNamespace =
        //       "http://foo.bar/wsd/translator/1.0/wsdl/TranslatorService";
        //   String method = "getTranslation";
        //   SoapObject soapRequest =
        //       new SoapObject(methodNamespace, method);
        //   soapRequest.addProperty("String_1", inputLocale); // parameter 1
        //   soapRequest.addProperty("String_2", outputLocale); // parameter 2
        //   soapRequest.addProperty("String_3", inputText); // parameter 3
        //   ... convert to UTF-8 bytes, prepend XML prolog, etc. ...
        //
        // Assuming you know what the 'canned' request looks like,
        // the approach below is a simpler way to construct and encode
        // trivial UTF-8 (or UTF-16) SOAP requests.

        String soapMessage =
            "<?xml version=\"1.0\" encoding=\"" + CHARSET + "\"?>\n" +
            "<SOAP-ENV:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
                "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " +
                "xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" " +
                "xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
            "<SOAP-ENV:Body " +
                "SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n" +
            "<getTranslation " +
                "xmlns=\"http://foo.bar/wsd/translator/1.0/wsdl/TranslatorService\" " +
                "id=\"o0\" " + "SOAP-ENC:root=\"1\">\n" +
                "<String_1 xmlns=\"\" xsi:type=\"xsd:string\">" +
                    inputLocale + "</String_1>\n" +
                "<String_2 xmlns=\"\" xsi:type=\"xsd:string\">" +
                    outputLocale + "</String_2>\n" +
                "<String_3 xmlns=\"\" xsi:type=\"xsd:string\">" +
                    inputText + "</String_3>\n" +
            "</getTranslation>\n" +            
            "</SOAP-ENV:Body>\n" +
            "</SOAP-ENV:Envelope>\n";
            
        return soapMessage.getBytes(CHARSET);
    }
    

    private void queueRequestGetTranslate()
        throws IOException, UnsupportedEncodingException
    {
        byte[] bytes = getTranslationSOAPRequest(inputLocale,
                                                 outputLocale,
                                                 inputTextField.getString());
        Hashtable httpRequestProperties = new Hashtable();
        httpRequestProperties.put("Content-Type", CONTENT_TYPE);
        httpRequestProperties.put("Content-Length",
                                  Integer.toString(bytes.length));
        // (Set SOAPAction here if needed.)

        midlet.getHttpPoster().sendHttpRequest(bytes,
                                               httpRequestProperties,
                                               this);
    }
    
    
    // Note: If you can't test the MIDlet with the matching JWSDP 1.0_01
    // JAX-RPC based web service sample implementation that was
    // provided, then by making some minor changes to the code
    // you might be able to modify it to work with an alternate 
    // similar web service. At the time of writing,
    // http://www.xmethods.net seems to provide a similar 
    // service, the source code changes might look like this:
    //   1) Change .jad's TranslatorService-URL:
    //        http://services.xmethods.net:80/perl/soaplite.cgi
    //   2) Set SOAP-Action property in the HTTP header:
    //        httpRequestProperties.put("SOAPAction",
    //            "urn:xmethodsBabelFish#BabelFish");
    //   3) Modify the SOAP request appropriately
    //      (method getTranslationSOAPRequest):
    //          method namespace: "urn:xmethodsBabelFish"
    //          method: "BabelFish"
    //          parameter 1 name: "translationmode"
    //          parameter 1 value: (inputLocale + "_" + outputLocale)
    //          parameter 2 name: "sourcedata"
    //          parameter 2 value: inputText
    //   4) Modify method handleHttpResponse to handle
    //      "BabelFishResponse" instead of
    //      "getTranslationResponse"
    //   Etc. ...

    
    // add an animated activityIndicator to the Form
    private void startActivityIndicator()
    {       
        removeCommand(translateCommand);
        addCommand(cancelCommand);

        if (size() == 2)
        {
            // add indicator at top of Form
            insert(0, activityIndicator);
        }

        // start animation
        aborting = false;
        doAnimation = true;
        animationThread = new Thread(this);
        animationThread.start();
    }


    // Stop the activityIndicator's animation and remove from Form
    private void stopActivityIndicator()
    {
        // stop animation
        aborting = true;
        doAnimation = false;
        
        if (size() == 3)
        {  
            // remove indicator from top of Form
            delete(0);
        }
        
        removeCommand(cancelCommand);
        addCommand(translateCommand);
    }


    // HttpPosterListener methods

    public void receiveHttpResponse(byte[] bytes)
    {
        stopActivityIndicator();
               
        SoapEnvelope resultEnvelope = new SoapEnvelope();
        try
        {
            ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
            Reader reader = new InputStreamReader(bis);
            XmlParser parser = new XmlParser(reader);

            resultEnvelope.parse(parser);
        }
        catch (IOException e)
        {
            midlet.translatorScreenError("Response Parsing Error: " +
                                          e.toString());
        }
        
        Object body = resultEnvelope.getBody();
        if (body instanceof SoapFault)
        {           
            SoapFault fault = (SoapFault) body;

            // The SoapFault fault string may be verbose.
            // It contains the 'TranslationService error' message.
            midlet.translatorScreenError(fault.toString());
        }
        else
        {
            // (Note: You normally parse the XML formatted response here
            // as needed: either the 'Document/Literal' or 'RPC/Encoded
            // SOAP response depending on your web service.            
            // The simpler approach below was used because
            // TranslationService's response is so simple,
            // something like this:
            //   <env:Envelope ... ><env:Body>
            //   <ans1:getTranslationResponse
            //    xmlns:ans1="http://foo.bar/wsd/translator/1.0/wsdl/TranslatorService">
            //   <result xsi:type="xsd:string">resultStringGoesHere</result>
            //   </ans1:getTranslationResponse>
            //   </env:Body></env:Envelope>
            
            String result = null;
            try
            {   
                String responseStr = new String(bytes, CHARSET);
                if (responseStr.indexOf("getTranslationResponse") != -1)
                {
                    String s = (String) resultEnvelope.getResult();
                    result = new String(s.getBytes(), CHARSET);
                    
                    outputStringItem.setLabel("Translation:");
                    outputStringItem.setText(result);
                }
                else
                {                                             
                    midlet.translatorScreenError(
                               "Unexpected response: " +
                               result);                           
                }
            }
            catch(UnsupportedEncodingException e)
            {
                midlet.translatorScreenError(e.toString());
            }
        }
    }


    public void handleHttpPosterError(String errorMessage)
    {
        stopActivityIndicator();

        midlet.translatorScreenError(
                   "Communication error:\n" + errorMessage);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -