Tushar
Tushar

Reputation: 5955

http post connection in android

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

Upvotes: 0

Views: 2922

Answers (2)

bharath
bharath

Reputation: 14463

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);
        }

Upvotes: 1

Nanne
Nanne

Reputation: 64429

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/

Upvotes: 0

Related Questions