Getting Target host must not be null or set in parameters in http get method for android

别等时光非礼了梦想. 提交于 2019-12-12 00:15:06

问题


Please help me to figure out my question answer i tried to find so many times on Google but i cant able to find its solution.

First i am getting this exception

 java.lang.IllegalArgumentException: Illegal character in query at index 130

than after i add this code in my code.

String restUrl = URLEncoder.encode(url, "UTF-8");
HttpGet httpget = new HttpGet(restUrl);

then i am getting this exception

java.lang.IllegalStateException: Target host must not be null, or set in parameters.

Please help me solve out from this exception. Please.

Here is my code.


回答1:


Android: howto parse URL String with spaces to URI object?

You should in fact URLEncode the "invalid" characters. Since the string actually contains the complete URL, it's hard to properly URL-encode it. You don't know which slashes / should be taken into account and which not. You cannot predict that on a raw String beforehand. The problem really needs to be solved at a higher level. Where does that String come from? Is it hardcoded? Then just change it yourself accordingly. Does it come in as user input? Validate it and show error, let the user solve itself.



来源:https://stackoverflow.com/questions/11272027/getting-target-host-must-not-be-null-or-set-in-parameters-in-http-get-method-for

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!