查询中的非法字符

时间:2013-03-03 16:01:01

标签: java android

在我的应用程序中,我可以使用以下代码运行此方法:

fahrtenStr = getHtml("http://myServer.com/abc/getFahrtenList.cgi?limit=15");

但是如果我尝试使用这个版本,要调用该方法,它不起作用:

String url = "http://myServer.com/abc/insert_fahrt.cgi?values="+startKM+"x"+endKM+"x"+fahrer;
getHtml(url);

我收到以下错误消息:

03-03 16:49:15.363: E/AndroidRuntime(21355): java.lang.IllegalArgumentException: Illegal character in query at index 65: http://myServer.com/abc/insert_fahrt.cgi?values=11003.4
03-03 16:49:15.363: E/AndroidRuntime(21355): x111111xSimon
03-03 16:49:15.363: E/AndroidRuntime(21355):    at java.net.URI.create(URI.java:727)
03-03 16:49:15.363: E/AndroidRuntime(21355):    at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:75)
03-03 16:49:15.363: E/AndroidRuntime(21355):    at net.x.y.fahrtenbuch.Uebersicht.getHtml(Uebersicht.java:255)
03-03 16:49:15.363: E/AndroidRuntime(21355):    at net.x.y.fahrtenbuch.Uebersicht$3$1.run(Uebersicht.java:153)

我的职能:

    public String  getHtml(String url) throws ClientProtocolException, IOException {
    HttpClient httpClient = new DefaultHttpClient();
    HttpContext localContext = new BasicHttpContext();
    HttpGet httpGet = new HttpGet(url);
    HttpResponse response = httpClient.execute(httpGet, localContext);
    String result = "";

    BufferedReader reader = 
        new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

    String line = null;
    while ((line = reader.readLine()) != null){
        result += line + "\n";
        // Toast.makeText(Connect.this, line.toString(), Toast.LENGTH_LONG).show();

    }
    return result;
}

最后我使用了变量:

//Variable values
    startKM: String="11003.4"
    endKM: String="11111"
    fahrer : "Simon"

1 个答案:

答案 0 :(得分:0)

试试这个

String url = "http://myServer.com/abc/insert_fahrt.cgi?values="+startKM+"x"+endKM+"x"+fahrer;
url.replaceAll(" ","%20");
getHtml(url);

空间在网址中不起作用 您的三个变量中可能有空格(startKM,endKM,fahrer) 在url空间中必须替换为%20