【问题标题】:IllegalArgumentException when I try execute request当我尝试执行请求时出现 IllegalArgumentException
【发布时间】:2011-09-03 23:22:18
【问题描述】:

在我的代码中,我在执行对服务器的请求的行中捕获了 IllegalArgumentException(索引 85 处的查询中的非法字符)。使用被构建为模式命令,其他任务完成正确但不是这样:

public CreateCommentTask(String barcodeId, String ball, String comment,
        String sign) {
    super(getApplicationUrl() + "?command=createComment" + "&barcodeId="
            + barcodeId + "&ball=" + ball + "&text=" + comment
            + "&sessionId=" + sign);
    // TODO Auto-generated constructor stub
}

所以,我只有地址和一些字符串格式的数据。我的应用程序在这一行崩溃:

HttpResponse response = client.execute(task.createRequest());

你有什么想法吗?

【问题讨论】:

  • 你传递给这个方法的字符串是什么? barcodeId、ball、comment、sign
  • 请参阅thisthis 了解android 中的url 编码。
  • @Geobits,我传递了这个字符串:StringbarcodeId = "4605246006340";字符串球=“10”;字符串注释 = java.net.URLEncoder.encode("Mycomment", "UTF-8");

标签: java android illegalargumentexception


【解决方案1】:

我希望您需要对参数进行 URL 编码(很可能是 comment 变量)。

编辑:您可以使用 java.net.URI 生成正确的查询。试试这个:

super(new URI(getApplicationUrl() + "?command=createComment" + "&barcodeId="
            + barcodeId + "&ball=" + ball + "&text=" + comment
            + "&sessionId=" + sign).toString());

【讨论】:

  • 这种方法对我没有帮助。是的,我必须使用 URLEncoder 进行评论,我这样做了 - java.net.URLEncoder.encode("My comment", "UTF-8"),但它不起作用。
  • “我的”和“评论”之间的空格被替换为“+”,但应该是“%20”
  • 你不想使用 URLEncoder,实际上你想使用 URI 组件编码规则。看看stackoverflow.com/questions/607176/…显示的类
【解决方案2】:

您为什么不事先构建字符串并记录它?然后你可以看到字符 85 是什么。我保证问题就在那里,如果这就是日志所说的。如果您不知道原因,请将生成的字符串连同日志的其余部分一起发布。

public CreateCommentTask(String barcodeId, String ball, String comment,
        String sign) {
    String query = getApplicationUrl() + "?command=createComment" + "&barcodeId="
            + barcodeId + "&ball=" + ball + "&text=" + comment
            + "&sessionId=" + sign;
    Log.d("HOLYCRAP", query);
    super(query);
}

【讨论】:

  • 哦,抱歉:我已经解决了这个异常的问题,但我发现了另一个 ClientProtocolException:服务器未能以有效的 HTTP 响应响应。我忘记检查我的问题是否已解决。
  • 您可能应该更新您的问题,以免造成更多混乱。而且你会想要提供更多信息,相信我
猜你喜欢
  • 1970-01-01
  • 2021-05-18
  • 2021-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-11
  • 1970-01-01
  • 2019-06-13
相关资源
最近更新 更多