【问题标题】:Escaping quotes in java在java中转义引号
【发布时间】:2012-11-09 21:49:34
【问题描述】:

我正在尝试在必须通过的地方进行网络服务调用

login.php?message=[{"email":"mikeymike@mouse.com","password":"tiger"}]

我已经使用反斜杠来转义这样的双引号

String weblink = "login.php?message=[{\"email\":\"mikeymike@mouse.com\",\"password\":\"tiger\"}]";

但我仍然遇到错误。我尝试过调用其他不需要带有任何双引号的数据的 Web 服务,它们工作正常,所以我很确定问题出在此。我也得到一个 java.lang 异常说

java.lang.Exception  Indicates a serious configuration error.DateParseException An exception to indicate an error parsing a date string.  DestroyFailedException Signals that the destroy() method failed         

编辑: 我尝试过使用 URLEncoder 和 JSON 对象,但仍然出现错误

这是剩下的代码

String HOST = "http://62.285.107.329/disaster/webservices/";

 String weblink = "login.php?message=[{\"email\":\"mikeymike@mouse.com\",\"password\":\"tiger\"}]";
String result = callWebservice(weblink);

 public String callWebservice(String weblink) {
    String result = "";
    try {

        HttpParams httpParameters = new BasicHttpParams();
        int timeoutConnection = 7500;
        HttpConnectionParams.setConnectionTimeout(httpParameters,
                timeoutConnection);
        int timeoutSocket = 7500;
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
        HttpClient client = new DefaultHttpClient(httpParameters);
        HttpGet request = new HttpGet();
        URI link = new URI(HOST + weblink);
        request.setURI(link);
        HttpResponse response = client.execute(request);

        BufferedReader rd = new BufferedReader(new InputStreamReader(
                response.getEntity().getContent()));
        result = rd.readLine();

    } catch (Exception e1) {
        e1.printStackTrace();
        result = "timeout";
    }
    return result;
}

Web 服务还返回一个 JSON 对象,所以这也可能是错误的原因吗?

【问题讨论】:

  • 异常没有意义。发布堆栈跟踪。
  • 如果您提供错误可能会有所帮助。

标签: java string http escaping


【解决方案1】:

与其手动尝试并得到错误,不如使用 JSONObject 类和 UrlEncoder 的组合。

 JSONObject json = new JSONObject();
 json.put("email","mikeymike@mouse.com" );
 json.put("password", "tiger");
 String s = "login.php?message=" + UrlEncoder.encode(json.toString());

【讨论】:

    【解决方案2】:

    您必须使用%22 代替",如:login.php?message=[{%22email%22:%22mikeymike@mouse.com%22,%22password%22:%22tiger%22}]

    " 不是 URL 中的有效字符。

    更通用的解决方案是使用URLEncoder.encode("login.php?message=[{\"email\":\"mikeymike@mouse.com\",\"password\":\"tiger\"}]", "UTF8")

    【讨论】:

    • URLEncoder 函数编码的字符比 "." 多得多,你确定它说的是无效字符吗?
    【解决方案3】:

    当您与 Web 服务通信时,您需要 URL encode 您的数据。在您的情况下,url 编码会将" 替换为%22,但如果您要添加其他特殊字符,例如%,编码也会捕获这些字符。 JDK中有一个java类,叫做URLEncoder

    所以,基本上,你要做的就是使用URLEncoder.encode() 准备你的字符串,就像这样:

    String weblink = URLEncoder.encode("login.php?message=[{\"email\":\"mikeymike@mouse.com\",\"password\":\"tiger\"}]");
    

    现在字符串已编码,您应该能够将其发送到服务器并让它理解您的意思。

    【讨论】:

      【解决方案4】:

      向所有人道歉,但问题似乎是我试图以错误的方式使用 Web 服务,因为它返回一个 JSON 对象。

      对于任何可能遇到此问题的人来说,正确的方法是在下面的代码中

      String str="url";
      try{
          URL url=new URL(str);
          URLConnection urlc=url.openConnection();
          BufferedReader bfr=new BufferedReader(new InputStreamReader(urlc.getInputStream()));
          String line;
          while((line=bfr.readLine())!=null)
          {
          JSONArray jsa=new JSONArray(line);
          for(int i=0;i<jsa.length();i++)
             {
             JSONObject jo=(JSONObject)jsa.get(i);
                          title=jo.getString("deal_title");  //tag name "deal_title",will return value that we save in title string
                      des=jo.getString("deal_description");
         }
      }
      catch(Exeption e){
      }
      

      这个答案来自

      How to call a json webservice through android

      【讨论】:

        猜你喜欢
        • 2011-09-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-15
        • 2013-07-06
        • 1970-01-01
        • 1970-01-01
        • 2012-08-10
        相关资源
        最近更新 更多