2012-10-18 16 views
0

我的J2ME midlet可以从PHP服务器中检索中文字符的消息,但是它是乱码。服务器基本上返回响应字符串并检测前2个字符。 AA =好,别的指示误差,其中该消息将被传递给调用功能显示j2me midlet中文字符显示消息乱码

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"); ?> 

什么可能是错的?

回答

2

我发现这个用户有同样的问题,它的回答是 Reading UTF8 strings from a server through http using 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();  
//       } 

我MIDP代码

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

只是想知道,但为什么读取字节搅乱UTF-8字符?