我尝试执行请求时出现IllegalArgumentException

时间:2011-05-28 14:45:58

标签: java android illegalargumentexception

在我的代码中,我在执行对服务器的请求的行中捕获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());

你有什么想法吗?

2 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分:0)

为什么不预先构建字符串并记录它?然后你可以看到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);
}