【问题标题】:http post connection in androidandroid中的http post连接
【发布时间】:2011-01-25 18:52:07
【问题描述】:

我正在 android 中开发一个应用程序。这涉及从应用程序向 android 中的 url/server 发送数据...有人可以告诉我如何处理这个...提前谢谢 图沙尔

【问题讨论】:

    标签: android http-post


    【解决方案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);
            }
    

    【讨论】:

    • bhakki 你上面提到的代码....在此先感谢图沙尔
    • 如果成功意味着你得到了服务器的响应。否则你得到的响应是空的或有任何异常。
    【解决方案2】:

    你所要做的就是搜索,这个问题已经被问过很多次了before

    【讨论】:

    • dave,,,,ok,,,,我知道了,,,,,你现在有android中http post连接的例子吗
    • 是的,我链接到的帖子有两个链接到 Android 中的两个 HttpPost 示例,以及一个纯 Java 示例和一个使用 URLConnection 的版本。
    【解决方案3】:

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

    Can anyone explain me this code?

    作为对您问题的回答:您的 URL 位于 httppost 构造函数中:

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

    您可以阅读本主题的其余部分以获取快速操作方法:http://www.androidsnippets.org/snippets/36/

    【讨论】:

    • 我会把它添加到答案中,但是快速谷歌也会给你答案......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-27
    相关资源
    最近更新 更多