【问题标题】:how to send string that contains % symbol to webservice using HttpPost in android?如何在 android 中使用 HttpPost 将包含 % 符号的字符串发送到 web 服务?
【发布时间】:2013-03-25 10:02:27
【问题描述】:

我需要使用 HttpPost 将包含 % (EX: abc%xyz) 的字符串发送到 Web 服务。

我使用了以下逻辑,但在 (%) 的位置出现了 IllegalCharacter 异常。

**updatepeople.php?pilotid=1651&firstname=Nexus&lastname=Google&nickname=abc%xyz**

       final int TIMEOUT_MILLISEC = 20000;
      HttpParams httpParams = new BasicHttpParams();
      HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
      HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
      HttpConnectionParams.setTcpNoDelay(httpParams, true);
      HttpClient httpClient = new DefaultHttpClient(httpParams);

       URLEncoder.encode(url, "UTF-8");
       HttpPost httpPost = new HttpPost(url);
  ResponseHandler<String> resHandler = new BasicResponseHandler();
  page = httpClient.execute(httpPost, resHandler);
   return page;

【问题讨论】:

  • 刚刚使用了 str.replaceAll("%","%25")
  • updatepeople.php?pilotid=1651&firstname=Nexus&lastname=Google&nickname=abc xyz

标签: android url http-post


【解决方案1】:

在查询字符串中传递值时尝试使用此代码,而不是将此作为实体传递。

现在您的网址只包含页面链接,没有任何参数,它将以 updatepeople.php

结尾
HttpPost httpPost = new HttpPost(url);

List<NameValuePair> param = new ArrayList<NameValuePair>();
param.add(new BasicNameValuePair("pilotid", "1651"));
param.add(new BasicNameValuePair("firstname", "Nexus"));
param.add(new BasicNameValuePair("lastname", "Google"));
param.add(new BasicNameValuePair("nickname", "abc%xyz"));

httpPost.setEntity(new Unew UrlEncodedFormEntity(param));

现在执行它并尽可能继续

【讨论】:

    猜你喜欢
    • 2012-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-18
    • 1970-01-01
    • 2011-08-02
    相关资源
    最近更新 更多