- Here i am using httpclient for getting response from a webservice, using httppost i am sending request , server data content type is application/x-www-form-urlencoded.
How to add that,here is my code,means i want only "application/x-www-form-urlencoded" this type of data from service
String postUrl = url; HttpClient client = new DefaultHttpClient(); HttpPost request = new HttpPost(postUrl); request.setHeader("Content-type", "application/x-www-form-urlencoded"); request.setHeader("Expect", "100-continue"); HttpResponse response = client.execute(request); System.out.println("ddddddddddddddddddddddd "+response); BufferedReader reader = new BufferedReader( new InputStreamReader( response.getEntity().getContent(), "UTF-8")); String line; builder = new StringBuilder(); while ((line = reader.readLine()) != null) { builder.append(line); } Log.d("Response", builder.toString()); resObj = builder.toString(); System.out.println("ddddddddddddddddddddddd "+resObj); } catch (Exception e) { // TODO: handle exception Log.d("ERROR", e.toString()); }
Asked
Active
Viewed 2,843 times
0

Chintan Khetiya
- 15,962
- 9
- 47
- 85

user2394220
- 1
- 1
- 4
-
i don't understand the question. – njzk2 Jun 03 '13 at 08:04
-
i used request.setHeader("Content-type", "application/x-www-form-urlencoded"); request.setHeader("Expect", "100-continue"); – user2394220 Jun 03 '13 at 08:04
-
and so, i don't understand what your question is. you apparently set the header, yet ask about how to add the header? – njzk2 Jun 03 '13 at 08:05
-
i added that but it is not wotking – user2394220 Jun 03 '13 at 08:07
-
i am not getting data from service – user2394220 Jun 03 '13 at 08:11
-
means no data from service – user2394220 Jun 03 '13 at 08:11
-
and you know for a fact this is due to a problem in your Content-Type header? how? – njzk2 Jun 03 '13 at 08:14
-
possible duplicate -http://stackoverflow.com/questions/6442791/send-http-get-request-with-header – Sharad Mhaske Jun 03 '13 at 08:14
-
you post nothing to th server ... for x-www-.... use http://developer.android.com/reference/org/apache/http/client/entity/UrlEncodedFormEntity.html to post something ... and dont you worry about Content Type ... UrlEncodedFormEntity will set it for you ... and one more don't bother about Sharad Mhaske comment he is wrong – Selvin Jun 03 '13 at 08:21
1 Answers
0
i tried using the httpost with namepair values ,it dosen't work, so came to conclusion using other way
its working good, Note : we need java 1.7 jre
String param1 = "9880938687";
String param2 = "value2";
// ...
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("mobile", param1));
String query = null;
try {
query = String.format("mobile=%s¶m2=%s",
URLEncoder.encode(param1, charset),
URLEncoder.encode(param2, charset));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
URLConnection connection = new URL(targetURL).openConnection();
connection.setDoOutput(true); // Triggers POST.
connection.setRequestProperty("Accept-Charset", charset);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);
try (OutputStream output = connection.getOutputStream()) {
output.write(query.getBytes(charset));
}
//InputStream response = connection.getInputStream();
// ..
//Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
System.out.println("***"+response.toString());
return response.toString();

Issac Balaji
- 1,441
- 1
- 13
- 25