2013-05-18 48 views
4

我在我的android应用程序中创建了一个HTTP-post。值从我的应用程序以字符串的形式发送到我的网络服务器。问题是,这些值不是UTF-8,因为我希望它们是。我的网络服务器有UTF-8编码,所以我知道我的应用程序中有代码需要更改。 见下面我摘录:在Android HTTP请求中发送utf-8编码的字符串

private void sendPostRequest(String facebookId, String name, String email) { 

     class SendPostReqAsyncTask extends AsyncTask<String, Void, String>{ 

      @Override 
      protected String doInBackground(String... bcs) { 

       String bcFacebookId = bcs[0]; 
       String bcName = bcs[1]; 
       String bcEmail = bcs[2]; 



       HttpClient httpClient = new DefaultHttpClient(); 

       HttpPost httpPost = new HttpPost("URL"); 

       BasicNameValuePair facebookIdBasicNameValuePair = new BasicNameValuePair("bcFacebookId", bcFacebookId); 
       BasicNameValuePair nameBasicNameValuePair = new BasicNameValuePair("bcName", bcName); 
       BasicNameValuePair emailBasicNameValiePair = new BasicNameValuePair("bcEmail", bcEmail); 

       List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>(); 
       nameValuePairList.add(facebookIdBasicNameValuePair); 
       nameValuePairList.add(nameBasicNameValuePair); 
       nameValuePairList.add(emailBasicNameValiePair); 
       try { 

        UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nameValuePairList); 

        httpPost.setEntity(urlEncodedFormEntity); 

        try { 
         HttpResponse httpResponse = httpClient.execute(httpPost); 

         InputStream inputStream = httpResponse.getEntity().getContent(); 

         InputStreamReader inputStreamReader = new InputStreamReader(inputStream); 

         BufferedReader bufferedReader = new BufferedReader(inputStreamReader); 

         StringBuilder stringBuilder = new StringBuilder(); 

         String bufferedStrChunk = null; 

         while((bufferedStrChunk = bufferedReader.readLine()) != null){ 
          stringBuilder.append(bufferedStrChunk); 
         } 

         return stringBuilder.toString(); 

        } catch (ClientProtocolException cpe) { 

         cpe.printStackTrace(); 
        } catch (IOException ioe) { 
         System.out.println("Second Exception caz of HttpResponse :" + ioe); 
         ioe.printStackTrace(); 
        } 

       } catch (UnsupportedEncodingException uee) { 
        System.out.println("An Exception given because of UrlEncodedFormEntity argument :" + uee); 
        uee.printStackTrace(); 
       } 

       return null; 
      } 

举一个例子,字母“O”变成一个“?”。我该如何解决? 干杯!

回答

7

字符转换为问号的最大原因是将字符转换为字节,然后再转换为字符,而不是匹配。

您提供的代码有这样一行:

InputStreamReader inputStreamReader = new InputStreamReader(inputStream); 

这是有问题的,因为你没有指定如何将字节转换成字符。相反,您可能需要:

InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8"); 

您为字符编码指定的内容将取决于您在别处指定的字符编码。如果不指定字符编码,您将获得“默认”字符编码,并且这取决于客户端和服务器中的设置。 Java使用Unicode,而UTF-8是唯一能够保留Java允许的所有字符的编码。

对于调试,您可能希望使用InputStream并从中检索字节,并打印出字节值,以验证它们确实是原始字符值的UTF-8编码表示形式。 'ö'(x00F6)的正确编码是'ö'(x00C3 x00B6)。

您还需要确保原始的POST请求是正确的UTF-8编码。 UrlEncodedFormEntity类也使用默认的字符编码,可能不是UTF-8。更改此:

UrlEncodedFormEntity uefe = new UrlEncodedFormEntity(nameValuePairList); 

UrlEncodedFormEntity uefe = new UrlEncodedFormEntity(nameValuePairList, "UTF-8"); 
+0

试过你的代码,没有解决它。 – Jack

+0

对不起,没有工作。我会建议输出字节值的调试语句,以便您可以确定您正在读取的流是否正确编码,并确定该编码是什么。 – AgilePro

+0

解决它,现在工作。谢谢! :) – Jack

0

如果数据库的编码设置正确+表编码设置正确+列编码设置不当,将所有的数据妥善保存。这是第一部分。现在第二个重要的部分 - 确保你的mysql连接后有这个命令:SET NAMES utf8

这是我遇到同样问题的情况。希望这也能为你工作。