Android HTTP GET parameters - android

I'm using the apache http library and need to know how to add a parameter to an HTTP GET request. I've looked over How to add parameters to a HTTP GET request in Android? but the accepted answer for that adds parameters to an HTTP POST. This is my code so far but it is not working.
HttpGet get = new HttpGet("https://server.com/stuff");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("count", "5"));
HttpParams p = get.getParams();
p.setParameter("length", "5");
get.setParams(p);

String url = "https://server.com/stuff"
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("count", "5"));
HttpClient httpClient = new DefaultHttpClient();
String paramsString = URLEncodedUtils.format(nameValuePairs, "UTF-8");
HttpGet httpGet = new HttpGet(url + "?" + paramsString);
HttpResponse response = httpClient.execute(httpGet);
EDIT: Since Android SDK v22, the type NameValuePair is deprecated. I recommend using Volley, an HTTP library that makes networking for Android apps easier and most importantly, faster.

unlike POST, GET sends the parameters under the url like this:
http://myurl.com?variable1=value&variable2=value2
Where: the parameters area start from the question mark and on so the variable1 is the first param and it has "value" value...
See here for more informations.
So what you need to do is just build an url that contains also these parameters according to server needs.
EDIT:
In your case :
HttpGet get = new HttpGet("https://server.com/stuff?count=5&length=5");
...
Where: count=5 and length=5 are the parameters and the "?" mark is the beginning of the parameters definition...
I hope that helps.

Related

Sending json string with Http GET request with HttpClient in android

I am using HttpClient library. And I know the way of sending the json string with POST request directly. Like:
HttpPost post = new HttpPost(url);
post.setEntity(new StringEntity(jsonString));
But how to send the same data with GET request. Since, there is no method like "setEntity(StringEntity entity)" in HttpGet class.
And I am willing to send the data without any particular key like I do with POST request.
You can use a list of NameValuePair objects and append it to the url :
List<NameValuePair> params = new LinkedList<NameValuePair>();
params.add(new BasicNameValuePair("param_name_1", "param_value");
params.add(new BasicNameValuePair("param_name_2", "param_value");
String paramString = URLEncodedUtils.format(params, "utf-8");
String url = url + paramString;
Now, just do the GET Request on this url.
So, basically, what is happening here is that you're appending the parameter string to the url, which you can even do manually. But this is just a safer, elegant way IMO.

How to transform this HttpPost into an Url

I have the following code that I would like to transform into a simple single URL so that (possibly) I can use Picasso for downloading images:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(Constants.GET_OTHER_PROFILE_PICTURE_URL);
nameValuePairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
UrlEncodedFormEntity form = new UrlEncodedFormEntity(nameValuePairs);
httppost.setEntity(form);
With a GET requests the parameters can be declared in the url like
<url>?<entry.getKey>=<entry.getValue>
The same parameters (as key value pairs separated by a colon) are passed to a POST request through the body.

Posting a url containing an underscore (_)

I am posting a url with params containg an underscore (_).
sample: http://sdsdsds_asasasahjhd.com/dsdsdsd/login.json?
I am posting it like this:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://sdsdsds_asasasahjhd.com/dsdsdsd/login.json?");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("key1", "value1"));
nameValuePairs.add(new BasicNameValuePair("key2", "value2"));
nameValuePairs
.add(new BasicNameValuePair("key3", "value3"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (Exception e) {
e.printStackTrace();
}
When I am inspecting httpclient.execute(httppost) I am getting IllegalArgumentException and in catch in exception details it is telling Host name cannot be null.
Please specify any solution.
I have gone through some other questions here:
java.lang.IllegalStateException: Target host must not be null, or set in parameters
Host name may not be null in HttpResponse execute for android
but no use as I am not encoding the whole url.
I have an open-source library with network implementation mechanism. It has just receiver an workaround implementation. All you need it to set the host by reflection in case of troubles:
final URI uriObj = new URI("https", host, path, null, null);
if (uriObj.getHost() == null) {
final Field hostField = URI.class.getDeclaredField("host");
hostField.setAccessible(true);
hostField.set(uriObj, host);
}
return uriObj;
The commit is here.
well it clearly states that the host name can not be null.. your url doesn't specify one..
a url is expected to be in the format
http://hostName.com/example..../example.json
HttpPost httppost = new HttpPost(String);
This contructor will attempt to form a URL instance from the provided String. If this fails, you'll be dealing with a null value.
Please post the actual URL you're using and not a sample if you want us to be able to help you further.
See this link. Apache doesnt support underscore. You should change the url
https://issues.apache.org/jira/browse/HTTPCLIENT-911

HTTPPOST to aspx file

I want to post to this url
http://abc.com/Registration.aspx?MailID=PickUp&UserName=as&PickUpTime=19191919&Notes=bla&DeviceId=0000
HttpPost httppost = new HttpPost("http://abc.com/Davis/Registration.aspx");
httppost.setHeader("MailID","MailID=PickUp");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
//nameValuePairs.add(new BasicNameValuePair("MailID","PickUp"));
nameValuePairs.add(new BasicNameValuePair("UserName","as"));
nameValuePairs.add(new BasicNameValuePair("PickUpTime",date));
nameValuePairs.add(new BasicNameValuePair("Notes",note));
nameValuePairs.add(new BasicNameValuePair("DeviceId",deviceID));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
Also how can I know what url I am passing . How can I log it ?
Are you sure MailID should be in the header? From the wording of the question, it looks as if all values are in the query string (in the URL past the ? mark). But then why would you need POST for that; a GET would be sufficient.
And passing data, like MailID, in headers is almost unheard of. Querystring and POST form, those are the most popular places.
So first figure out the interface of the server page. Does it expect GET or POST (or either)? Then place the fields into the right place - either into the URL (by string concatenation), or into the entity.
Oh, and the URL you're passing is http://abc.com/Davis/Registration.aspx. Neither setHeader() nor setEntity() modifies the URL per se.

BasicNameValuePair not working with # (at symbol) in value - Trying to create an HTTP POST request in Android

I am trying to create an HTTP POST request in android where I pass "username" and "password" as POST variables.
I have code that works under most circumstances. However when the password contains the "#" symbol it no longer works.
Here is my code
HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpClient httpClient = new DefaultHttpClient(params);
HttpPost post = new HttpPost(url);
List<NameValuePair> postData = new ArrayList<NameValuePair>();
postData.add(new BasicNameValuePair("username", "the_username"));
postData.add(new BasicNameValuePair("password", "the_password"));
post.setEntity(new UrlEncodedFormEntity(postData, HTTP.UTF_8));
HttpResponse response = httpClient.execute(post);
If "the_password" contains an #, such as "the_password#" then I'm not sure what the POST sends over the wire, but I get an error from the server.
The documentation indicates that certain special characters have to be escaped, but I'm not what API call to use to do that - and I have had no luck trying to manually escape the characters in my hard coded string.
Further more if I replace the following line:
post.setEntity(new UrlEncodedFormEntity(postData, HTTP.UTF_8));
with
post.setEntity(new StringEntity("username=the_username&password=the_password#", HTTP.UTF_8));
Then everything works - but this isn't a great solution because username and password are user entered and I'm unsure as to how that will work if they pass in a string with ampersand (&) or equals(=) in it
Any thoughts??
Are you sure your server correctly handles URL-escaped parameters?
If I run this:
List<NameValuePair> postData = new ArrayList<NameValuePair>();
postData.add(new BasicNameValuePair("username", "the_username"));
postData.add(new BasicNameValuePair("password", "the_password#"));
String urlencoform = EntityUtils.toString(new UrlEncodedFormEntity(postData, HTTP.UTF_8));
String directstring = EntityUtils.toString(new StringEntity("username=the_username&password=the_password#", HTTP.UTF_8));
I get:
urlencoform = "username=the_username&password=the_password%40"
directstring = "username=the_username&password=the_password#"
So this means it "works" with your server if you're sending unencoded parameters. This can't be good.
Any change to check what the server is receiving and how it does/does not handle it?

Categories

Resources