2011-03-14 151 views
5

我嘗試使用下面的代碼來執行以下鏈接:安卓HTTPGET問題

class httpget{ 
HttpGet httpGet=null; 

public void linkexecute(){ 
String url="http://<server>/<path>/action=send&msg=new message"; 
httpGet= new HttpGet(url); // line 1 
.... 
} 

at line 1 it is giving error "Illegal arguement exception"  
java.lang.IllegalArgumentException: Illegal character in query at index 77: http://<server>/<path>/sms.json?action=send&msg=new message  
at java.net.URI.create(URI.java:970)  
at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:75)  
at com.sms.login.LoginService.sendSms(LoginService.java:143) 

而有低於其在的「味精=」

String url="http://<server>/<path>/action=send&msg=newmessage"; 
話沒有間隙URL發出任何錯誤

如何解決URL中文字間隙問題?

+0

你能發佈完整的網址嗎? – 2011-03-14 06:19:30

回答

1

我認爲你應該使用20%而不是空間

+0

是的,做一個字符串替換「」到%20。 – sinni800 2011-03-14 06:22:40

+0

當你在你的url中只有空間的時候它很好,但是還有很多其他的字符,然後給IllegalCharacterException。 – 2012-09-27 15:44:18

15

這裏,我給大家一個函數,會從URL中刪除所有無效字符。請在此函數中傳遞您的網址,您將獲得帶有編碼字符串的新網址。

public static String convertURL(String str) { 

    String url = null; 
    try{ 
    url = new String(str.trim().replace(" ", "%20").replace("&", "%26") 
      .replace(",", "%2c").replace("(", "%28").replace(")", "%29") 
      .replace("!", "%21").replace("=", "%3D").replace("<", "%3C") 
      .replace(">", "%3E").replace("#", "%23").replace("$", "%24") 
      .replace("'", "%27").replace("*", "%2A").replace("-", "%2D") 
      .replace(".", "%2E").replace("/", "%2F").replace(":", "%3A") 
      .replace(";", "%3B").replace("?", "%3F").replace("@", "%40") 
      .replace("[", "%5B").replace("\\", "%5C").replace("]", "%5D") 
      .replace("_", "%5F").replace("`", "%60").replace("{", "%7B") 
      .replace("|", "%7C").replace("}", "%7D")); 
    }catch(Exception e){ 
     e.printStackTrace(); 
    } 
    return url; 
} 
+0

有這方法... – njzk2 2012-09-12 12:17:45

+0

通過我的計數,這創建了大約30個字符串對象...毛重。 – 2012-12-05 17:10:04

12

你絕對應該使用URLEncoder.encode(String, String)

1

的URLEncoder的並沒有爲我工作了,當我張貼變量我的web服務,因爲它是更換所有的參數。最後我用

URI uri = new URI("http", "server", "/path", "action=send&msg=newmessage", null); 
String url = uri.toASCIIString(); 

,後來使用httpPost與HttpClient的...

String xml = null; 
DefaultHttpClient httpClient = new DefaultHttpClient(); 
HttpPost httpPost = new HttpPost(url); 

HttpResponse httpResponse = httpClient.execute(httpPost); 
HttpEntity httpEntity = httpResponse.getEntity(); 
xml = EntityUtils.toString(httpEntity); 

解析我下載的數據我認爲HTTPGET方法相同的方式工作。

5

我認爲問題出在URL的參數上。相反,編碼或更換整個URL的,只是編碼參數應用這樣的:

Url: http://myHost.com/mail.do?address=New York [email protected]&time=12$3; 
right url:http://myHost.com/mail.do? + UrlEncode(address) + "&" + UrlEncode(time); 

另一個例子是:

Map<String, String> params = new HashMap<String, String>(); 
params.put("email", URLEncoder.encode(loginStr)); 
params.put("pass", URLEncoder.encode(passwStr)); 
Model.doAuthUser(params, userCallback); 

Model.doAuthUser loks類似以下內容:

String url = "http://myHost.com/mail.do"; 
if (params != null) { 
url += "?"; 
Boolean beginAddParams = true; 
for (Entry<String, String> entryParams : params.entrySet()) { 
if (!beginAddParams) { 
    url +="&"; 
    } else { 
    beginAddParams = false; 
    } 
    url += entryParams.getKey() + "=" + entryParams.getValue(); 
} 

,並使用url無論你想要什麼。