0

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

bharath
  • 14,150
  • 16
  • 55
  • 94
User
  • 5,861
  • 15
  • 47
  • 80

3 Answers3

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,150
  • 16
  • 55
  • 94
  • 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 – User 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/

Community
  • 1
  • 1
Nanne
  • 63,347
  • 16
  • 116
  • 159
0

All you have to do is search, this question has been asked many times before.

Community
  • 1
  • 1
dave.c
  • 10,907
  • 5
  • 38
  • 61
  • dave,,,,ok,,,,i got it,,,,,do u have any example of http post connection in android now – User Jan 25 '11 at 11:36
  • Yes, the post I linked to has links to two `HttpPost` examples in Android, as well as a pure Java example and a version using `URLConnection`. – dave.c Jan 25 '11 at 11:41