Reputation: 37657
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}¶mVersion={osversion}
This is my code:
String url = "http://www.myurl.com/~or/senddata.php?paramDevice={deviceInfo}¶mVersion={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
Upvotes: 1
Views: 154
Reputation: 3679
You need to encode the parameter only, not the whole url :
String url = "http://www.myurl.com/~or/senddata.php?paramDevice="+URLEncoder.encode("{deviceInfo}")+"¶mVersion="+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();
}
Upvotes: 2
Reputation: 9
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);
Upvotes: 0