android中的http post连接

时间:2011-01-25 10:42:32

标签: android http-post

我正在android中开发一个应用程序。其中涉及从应用程序向android中的url / server发送数据...有人能告诉我如何追求这个......提前谢谢 图莎尔

3 个答案:

答案 0 :(得分:1)

请参阅此示例代码。它会帮助你。

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

答案 1 :(得分:0)

您可以使用此问题中的代码。我已经解释了它如何作为这个问题的答案:)

Can anyone explain me this code?

作为您问题的答案:您的网址位于httppost构造函数中:

 new HttpPost("http://www.yoursite.com/script.php");  

您可以阅读本主题的其余部分,以便快速了解:http://www.androidsnippets.org/snippets/36/

答案 2 :(得分:0)

您所要做的就是搜索,这个问题已被多次询问before