0

我的 J2ME midlet 可以从 PHP 服务器检索中文字符的消息,但它是乱码。服务器基本上返回响应字符串并通过检测前 2 个字符。AA = 好,其他任何表示错误的消息将传递给调用函数进行显示

    InputStream is = null;
    StringBuffer sb = null;
        String str = "";
    HttpConnection http = null;
        DataOutputStream dos = null;
    try
    {
                URL = login.getURL();
        URL += ctlFunction + "/" + uriStr;
        URL = EncodeURL(URL);
                //System.out.println(URL);
                if(!ctlFunction.equals("login"))
                {
                    msg += "&user=" + login.getUsername();
                    msg += "&token=" + login.getToken();
                }
                msg += "&lang=" + System.getProperty("microedition.locale");
        // establish the connection
        http = (HttpConnection) Connector.open(URL);
        http.setRequestMethod(HttpConnection.POST);
                http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                http.setRequestProperty("Content-length", ""+EncodeURL(msg).getBytes().length);
                dos = http.openDataOutputStream();
                byte[] request_body = EncodeURL(msg).getBytes();
                for (int i = 0; i < request_body.length; i++)
                {
                        dos.writeByte(request_body[i]);
                }                        
        // server response
        if (http.getResponseCode() == HttpConnection.HTTP_OK)
        {
                    is = http.openDataInputStream();
                    int length = (int) http.getLength();
                    if (length != -1)
                    {
                        // Read data in one chunk
                        byte serverData[] = new byte[length];
                        is.read(serverData);
                        str = new String(serverData);
                    }
                    else  // Length not available...
                    {
                        ByteArrayOutputStream bStrm = new ByteArrayOutputStream();
                        int ch;
                        while ((ch = is.read()) != -1)
                            bStrm.write(ch);

                        str = new String(bStrm.toByteArray());
                        bStrm.close();    
                    }
        }
        else
        {
            networkError();
        }
    }
    catch (Exception e)
    {
        System.err.println("Error3: " + e.toString());
        networkError(e.toString());
    }
    finally
    {
        if (is != null)
            is.close();
        if (!str.equals(""))
            post = str;
        else
            networkError();
        if (http != null)
            http.close();
    }

    if (post != null)
    {
            String fate = post.substring(0, 2);
            if(fate.equals("AA"))
            {
                if(ctlFunction.equals("login"))
                {
                    String rawPost = post.substring(2);
                    Vector v = new Vector();
                    int index = 0;
                    //find the first occurrence of the SPLITTER
                    int endIndex = rawPost.indexOf(SPLITTER, index);
                    String item = "";
                    //extract the items until the end of the last SPLITTER found in the rawPost string
                    while(endIndex != -1)
                    {
                        item = rawPost.substring(index, endIndex);
                        index = endIndex + 1;
                        endIndex = rawPost.indexOf(SPLITTER, index);
                        v.addElement(item);
                    }
                    //extract the rest of the rawPost (the text item)
                    item = rawPost.substring(index);
                    v.addElement(item);
                    String[] ret = new String[v.size()];
                    v.copyInto(ret);
                    login.setToken(ret[0]);
                    login.setToday(ret[1]);
                    login.setNextDrawDay(ret[2]);
                }
        midlet.returnResults(post.substring(2), getCurrentDisplay(), ctlFunction);
            }
            else
            {
                String errmessage = post.substring(2);
                System.out.println(post);
                midlet.showInfo(post, getCurrentDisplay());
            }
    }
    else
    {
        networkError();
    }

在 PHP 服务器上,我已将标头设置为 UTF-8 编码

<?php header("Content-Type:text/plain; charset=utf-8"); ?>

什么可能是错的?

4

1 回答 1

2

我发现这个用户有同样的问题,并且已经回答 Read UTF8 strings from a server through http using MIDP。感谢答案。

我基本上编辑了我的 MIDP 代码

//                            is = http.openDataInputStream();
//                            int length = (int) http.getLength();
//                            if (length != -1)
//                            {
//                                // Read data in one chunk
//                                byte serverData[] = new byte[length];
//                                is.read(serverData);
//                                str = new String(serverData);
//                            }
//                            else  // Length not available...
//                            {
//                                ByteArrayOutputStream bStrm = new ByteArrayOutputStream();
//                                int ch;
//                                while ((ch = is.read()) != -1)
//                                    bStrm.write(ch);
//
//                                str = new String(bStrm.toByteArray());
//                                bStrm.close();    
//                            }

                    Reader r = new InputStreamReader(http.openInputStream(), "UTF-8");
                    int ch;
                    while((ch = r.read()) != -1)
                        str = str + (char)ch;

只是想知道为什么读取字节会弄乱 UTF-8 字符?

于 2012-10-18T03:53:34.750 回答