I'm on Android 1.5, and my code is like this:
HttpPost httpPost = new HttpPost(url);
HttpEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
HttpEntity respEntity = response.getEntity();
String result = EntityUtils.toString(respEntity, DEFAULT_CHARSET);
After successfully executed these codes, the result is a stripped string. I've tried using browser to test the url+param, it works fine and got all data.
What's wrong with this code? Is there any parameters I need to specified?
Try using the ResponseHandler pattern instead and see if that gives you better results.
This works:
HttpResponse response=httpclient.execute(httppost);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
Related
I have a problem when I tried to post my data it logged as :
[ObserverTRID=5QEET3TE10,
ObsType=Evaluate,
ObsDate=17-Jul-2014,
ObsTime=09:22:12,
// == loc
ObsTitle=bolllloooo,
//==obstset
MediaData={"MediaData":[{"RunNo":0,"Filename":"http://url/5QEET3TE10_3_5_20140717092206.jpg","MediaNo":4"Desc":01 Image_17-Jul-14No Marker.jpg}]},
PVPMediaCount=1,
SourceDevice=motorola XT1032 Android v19 App v2.0001,
ObsDept,
ObsSite,
TargetTRID=5QEET3TE10]
But I need to append every namevalue with "&" instead of "," This is my httppost request.
httppost.setEntity(new UrlEncodedFormEntity(mNameValuePairs));
This is my request i am posting the URL with the parameter nameevaluepair
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(mUrl);
if(mNameValuePairs==null){
mNameValuePairs = new ArrayList<NameValuePair>(1);
mNameValuePairs.add(new BasicNameValuePair("PostEvent", "Null"));
}
Log.e("namevalue ", mNameValuePairs.toString());
There I am settig entity by URLEncoding
httppost.setEntity(new UrlEncodedFormEntity(mNameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
String result = EntityUtils.toString(response.getEntity(),HTTP.UTF_8);
HttpEntity httpEntity = response.getEntity();
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
use this code in for parsing class
I want to make a request to a page that contains a .csv file, get it back and parse it. I've been searching a lot and I found a lot of questions about accessing secure connections and stuff, but I think my problem is different. Below is my code:
protected String doInBackground(Void... params) {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("https://recursos-data.buenosaires.gob.ar/bicicletas-publicas/estaciones-bicis.csv");
String text = null;
try {
HttpResponse response = httpClient.execute(httpGet, localContext);
Log.v("response code", response.getStatusLine()
.getStatusCode() + "");
HttpEntity entity = response.getEntity();
When I debug it and get to the line HttpResponse response = httpClient... the program doesn't throw any exception but it doesn't keep on going either. It just hangs there. I don't know what the problem is. I've used this code before.
I'm using Android 2.2. Any ideas?
Hi :) I got a simple problem, but very annoying.
I'm trying to send http post request using HttpPost class
This is part of the method which returns InputStream:
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url+path);
ArrayList<NameValuePair> paramsList = new ArrayList<NameValuePair>();
paramsList.add(new BasicNameValuePair("key1", value1));
paramsList.add(new BasicNameValuePair("key2", value2));
paramsList.add(new BasicNameValuePair("key3", value3));
httpPost.setEntity(new UrlEncodedFormEntity(paramsList));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
InputStream is = httpEntity.getContent();
return is;
But the problem is on the server, because server "thinks" that im sending GET request instead of POST. The question is: where is mistake? Im using similar part of code in other application and it works fine.
Cheers. :)
Please try
httpPost.setEntity(new UrlEncodedFormEntity(paramsList, HTTP.UTF_8));
Instead Of
httpPost.setEntity(new UrlEncodedFormEntity(paramsList));
I'm currently working on a project which needs to send a post request and get a json object from the server. Earlier I used Get method to access the json object. It worked fine. But because of some server changes I had to move to post method. Then it doesn't return me the json object that I got earlier from the 'get' method. I tried my best to come up with a solution but couldn't. Highly appreciate if anyone can help me to get through this problem.
private AdSniperAdObjectResponse postData(String url) {
//Bundle b = new Bundle();
HttpClient httpClient = HttpClientFactory.getThreadSafeClient();
//Log.d(TAG, "url: " + url);
try {
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", "application/json");
httpPost.setHeader("Accept", "JSON");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("latitude", "-33.8736"));
nameValuePairs.add(new BasicNameValuePair("longitude", "151.207"));
nameValuePairs.add(new BasicNameValuePair("age", "35"));
nameValuePairs.add(new BasicNameValuePair("gender", "All"));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity resEntity = httpResponse.getEntity();
if (resEntity != null) {
String resp = EntityUtils.toString(resEntity);
Above is the code that I use. Earlier I used HttpGet class. For HttpPost, the 'resp'variable is always null. Don't know what I did wrong.
should't this be like
HttpResponse httpResponse = httpClient.execute(httpPost);
if (httpResponse != null) {
String resp = httpResponse.toString();
and in case if server return JSONString..
say JSONObject data = new JSONObject(resp);
and then get values..
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof (List<NameValuePair> ));
try with this and pass your data using this
jsonSerializer.WriteObject(reqStream, nameValuePairs );
reqStream.Close();
and again deserialize the response whatever you are getting
Before you attempt to get the HttpEntity, you should get the StatusLine and check that the status code is what you expect. I suspect that the real problem is that the server is sending an error response of some kind. And since you used an "Accept" header to request a JSON response, it is likely that the server is not sending any diagnostics in the response body ... so it is empty.
Guys I found the solution. It worked when I commented the following two lines.
httpPost.setHeader("Content-Type", "application/json");
httpPost.setHeader("Accept", "JSON");
So thanks everyone for your answers. Highly appreciate.
I am trying to set some Http parameters in the HttpPost object.
HttpPost post=new HttpPost(url);
HttpParams params=new BasicHttpParams();
params.setParameter("param", "value");
post.setParams(params);
HttpResponse response = client.execute(post);
It looks like the parameter is not set at all. Do you have any idea why this is happening?
Thank you
For those who hopes to find the answer using HttpGet, here's one (from https://stackoverflow.com/a/4660576/330867) :
StringBuilder requestUrl = new StringBuilder("your_url");
String querystring = URLEncodedUtils.format(params, "utf-8");
requestUrl.append("?");
requestUrl.append(querystring);
HttpClient httpclient = new DefaultHttpClient();
HttpGet get = new HttpGet(requestUrl.toString());
NOTE: This doesn't take in consideration the state of your_url : if there is already some parameters, if it already contains a "?", etc. I assume you know how to code/search and will adapt regarding your case.
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("param", "value"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
httpClient.execute(httpPost);