I am developing an application in android.Which involves sending data to url/server in android from application...Can someone tell me how to pursue with this....Thanks in advance Tushar
Asked
Active
Viewed 2,921 times
2 Answers
1
See this sample code. It will helps you.
HttpContext localContext = new BasicHttpContext();
String ret = null;
HttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY,
CookiePolicy.RFC_2109);
HttpPost httpPost = new HttpPost(url);
HttpResponse response = null;
StringEntity tmp = null;
httpPost.setHeader(
"Accept",
"text/html,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
try {
tmp = new StringEntity(data, "UTF-8");
} catch (UnsupportedEncodingException e) {
Log.e("Your App Name Here",
"HttpUtils : UnsupportedEncodingException : " + e);
}
httpPost.setEntity(tmp);
try {
response = httpClient.execute(httpPost, localContext);
if (response != null) {
ret = EntityUtils.toString(response.getEntity());
Log.i("result", ret);
}
} catch (Exception e) {
Log.e("Your App Name Here", "HttpUtils: " + e);
}

bharath
- 14,283
- 16
- 57
- 95
-
bhakki the code you have mentioned above .... I have used that code,,,How can i check whether the values i have passed into url are actually passed or not?? Thanks in advance Tushar – Tushar Jan 25 '11 at 11:02
-
if it is success means you got the response from server. Otherwise you got the response is null or got any exception. – bharath Jan 25 '11 at 11:09
0
You could use the code in this question. I've explained how it works a bit as an answer to that question :)
Can anyone explain me this code?
As an answer to your question: your URL goes in the httppost constructor:
new HttpPost("http://www.yoursite.com/script.php");
You can read the rest of this topic for a quick howto: http://www.androidsnippets.org/snippets/36/
-
I'll add it to the answer, but a quick google would've gotten you the answer too... – Nanne Jan 25 '11 at 10:49