Question

i'm trying to send parameteris with GET to a script, but i'm receiving this error:

java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=http://www.myurl.com/~or/senddata.php?paramDevice={deviceInfo}&paramVersion={osversion}

This is my code:

String url = "http://www.myurl.com/~or/senddata.php?paramDevice={deviceInfo}&paramVersion={osversion}";
String encodedUrl = URLEncoder.encode(url,"UTF-8");
HttpClient httpclient = new DefaultHttpClient();
httpclient.execute(new HttpGet(encodedUrl));

I see some other questions with this exception but all of them have very huge answers with classes and complicated methods, and i just want a minimalistic httpget conection that sends these two parameters to an script. I can't fit these answers with this code.

What am i doing wrong in my code?

Thanks

Was it helpful?

Solution

You need to encode the parameter only, not the whole url :

String url = "http://www.myurl.com/~or/senddata.php?paramDevice="+URLEncoder.encode("{deviceInfo}")+"&paramVersion="+URLEncoder.encode("{osversion}");
    try {
         HttpClient httpclient = new DefaultHttpClient();
         httpclient.execute(new HttpGet(url));

    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

OTHER TIPS

Please specify host in request. Do as following:

URIBuilder builder = new URIBuilder();

builder.setHost("myhost.com").setPath("/views");

URI uri = builder.build();

HttpGet httpget = new HttpGet(uri);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top