2011-09-09 55 views
5

我已經構建了一個android應用程序,讓你在網站上發佈你的名字,雖然我向表單發送一個http POST請求到網站。問題是我的客戶中有90%是瑞典人,而POST請求似乎是在一個字符串中包含特殊字符本身之後的特殊字符之後的所有內容。特殊字符從android手機的POST請求中消失

所以瑞典的姓氏「Börjesson」變成了「B」。

我POST請求代碼:

public static String execRequest(String url, Map<String, String> params) 
{ 
    try { 
     DefaultHttpClient defaultHttpClient = new DefaultHttpClient(); 
     HttpPost httpPost = null; 
     HttpGet httpGet = null; 
     if(params == null || params.size() == 0) { 
      httpGet = new HttpGet(url); 
      httpGet.setHeader("Accept-Encoding", "UTF-8"); 
     } 
     else { 
      httpPost = new HttpPost(url); 
      httpPost.setHeader("Accept-Encoding", "UTF-8"); 
       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
      for(String key: params.keySet()) { 
       nameValuePairs.add(new BasicNameValuePair(key, params.get(key))); 
      } 
      httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
     } 
     HttpResponse httpResponse = (HttpResponse)defaultHttpClient.execute(httpPost == null ? httpGet : httpPost); 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     if(null != httpEntity) { 
      InputStream inputStream = httpEntity.getContent(); 
      Header contentEncoding = httpResponse.getFirstHeader("Content-Encoding"); 
      if(contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("UTF-8")) { 
       inputStream = new GZIPInputStream(inputStream); 
      } 
      String responseString = convertStreamToString(inputStream); 
      inputStream.close(); 
       return responseString; 
     } 
    } 
    catch(Throwable t) { 
     t.printStackTrace(); 
    } 
    return null; 
} 

所以,我在做什麼錯什麼祕訣?

在此先感謝!

+1

做你試過'httpPost.setEntity(新UrlEncodedFormEntity(namevaluepairs中), 「UTF-8」);' – Selvin

+0

我找不到有參數的任何功能... – Mockarutan

+1

該死的我的壞不是setEntity bat UrlEncodedXXX構造函數'httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs,「utf-8」));' – Selvin

回答

6

使用httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "utf-8"));

+0

作爲社區wiki發佈答案。請接受它。 – Yury