0

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

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"
4

1 回答 1

0

尝试这个

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

url 中的空格无效 您可能在 url 空间中的三个变量(startKM、endKM、fahrer)中有空格必须替换为 %20

于 2016-10-12T21:28:29.750 回答