-1

我有一点麻烦,我需要创建一种将字符串发送到服务器的方法,我有点抱怨系统

            try {
                String errMsg = validateData();
                if(errMsg == null){
                    String mailURL = CLIENT_CONTACT_URL_MAIL_SERVICE;
                    mailURL = Utils.replaceAll(mailURL, "@toAddress", TO_DEFAULT_ADDRESS);
                    String content = "";
                    content = "Nombre: " + this.names.getText() + "\n";
                    content += "Apellido: " + this.surname.getText() + "\n";
                    content += "Email: " + this.email.getText() + "\n";
                    content += "Telefono: " + this.phone.getText() + "\n";
                    content += "Mensaje: " + this.complains.getText() + "\n";
                    mailURL = Utils.replaceAll(mailURL, "@bodyContent", URLUTF8Encoder.encode(content));
                    Utils.getWebData(mailURL, this);
                }else{
                    Dialog.alert(errMsg);
                }
            } catch (IOException e) {
                Logger.logErrorEvent("Error while sending client contact mail");
            }
            break;
4

1 回答 1

0

以下代码将数据发送到服务器。

        static String responce;
        static String httpURL;
        httpURL=your_server_url+content;  //here add your server url like- http://www.google.com/ then append the string content.
        try {
        if ((WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED)
                && RadioInfo
                        .areWAFsSupported(RadioInfo.WAF_WLAN)) {
            httpURL += ";interface=wifi"+";ConnectionTimeout=30000";
        }
        //Dialog.alert(httpURL);
        HttpConnection httpConn;
        httpConn = (HttpConnection) Connector.open(httpURL);
        httpConn.setRequestMethod(HttpConnection.POST);
        DataOutputStream _outStream = new DataOutputStream(httpConn.openDataOutputStream());
        byte[] request_body = httpURL.getBytes();
        for (int i = 0; i < request_body.length; i++) {
            _outStream.writeByte(request_body[i]);
        }
        DataInputStream _inputStream = new DataInputStream(
        httpConn.openInputStream());
        StringBuffer _responseMessage = new StringBuffer();
        int ch;
        while ((ch = _inputStream.read()) != -1) {
            _responseMessage.append((char) ch);
        }
        String res = (_responseMessage.toString());
        responce = res.trim();
        //Dialog.alert(responce);
        httpConn.close();

    }catch (Exception e) {
        Dialog.alert("Connection Time out");
    }

    return responce;
于 2013-08-27T03:07:57.663 回答