简体   繁体   中英

Square brackets causing error in URL string in android

Hi im having a problem with executing a URL in Android, I am sure it is related to the square brackets but I cant find any solution. Any suggestions would be welcome.

  protected String doInBackground(String... arg0) {
        try {


            int indexdevice = 12;
            String uuu = URLEncoder.encode  ("http://<ipaddress>/ZWaveAPI/Run/devices[2].instances[0].commandClasses[0x25].Set(255)", "UTF-8");
            HttpClient Client = new DefaultHttpClient();
            String SetServerString = "";
            HttpGet httpget = new HttpGet(uuu);
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            SetServerString = Client.execute(httpget,responseHandler);
            Log.v("NAS", "--------- amount is  " + SetServerString);

        } 
            catch (Exception ex) 
            {
            Log.v("NAS",String.valueOf(ex));
            }

The error I am getting is:

 07-08 12:37:33.970: V/NAS(1800): java.lang.IllegalStateException: 
Target host must not be null, or set in parameters. scheme=null, host=null, 

You are url encoding the complete url, including http and hostname. That won't work. Just encode the part after the host address:

String uuu = "http://<ipaddress>/"+URLEncoder.encode  ("ZWaveAPI/Run/devices[2].instances[0].commandClasses[0x25].Set(255)", "UTF-8");

the http:// is probably getting encoded try something like String foo = foo.replaceFirst("http://", ""); foo = "http://"+ foo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM