Android 将数据发送回服务器

发布于 2024-10-27 07:23:04 字数 280 浏览 0 评论 0原文

如何从 Android 应用程序将数据发送回服务器?

我已经尝试使用 HttpPost 并发布回 RESTful WCF 服务,但我无法让它工作(我已经创建了一个关于此的问题,但没有找到解决方案..) - 无论我做什么,我都会保留得到 405 Method not allowed 或 400 Bad Request.. :(

我不一定要求完整的代码示例.. 只是一个方向指针,它可以使我将数据发送回服务器。

重要的是用户不应该有允许或拒绝转移..可以说,它应该在幕后发生

提前致谢

How would one go about sending data back to server, from an android application?

I've already tried using HttpPost and posted back to a RESTful WCF service, but I couldnt get that to work (I've already created a SO question about this, without finding the solution..) - No matter what I do I keep getting 405 Method not allowed or the 400 Bad Request.. :(

I'm not asking for full code example necessarily.. just a pointer in a direction, which can enable me to send data back to a server.

It is important that the user should not have to allow or dismiss the transfer.. it should happen under the covers, so to speak

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

停顿的约定 2024-11-03 07:23:04

服务是出路。 REST(我在 Android 上推荐这个),或者基于 SOAP。有大量关于让 Android 应用程序与服务进行通信的教程,甚至使用 .net / wcf 的教程也是如此。

不过,您始终可以打开原始套接字并使用某些自定义协议发送数据。

编辑:

这是我的 asynctask 处理 http post 通信的 doInBackground 部分,也许会有帮助:

protected String doInBackground(String... req) {

        Log.d(TAG, "Message to send: "+req[0]);
        HttpPost p = new HttpPost(url);

        try{
            p.setEntity(new StringEntity(req[0], "UTF8"));
        }catch(Exception e){
            e.printStackTrace();
        }
        p.setHeader("Content-type", "application/json");

        String response = "";
        try{
            HttpResponse resp = hc.execute(p, localContext);
            InputStream is = resp.getEntity().getContent();
            response = convertStreamToString(is);
            Log.d("Response", "Response is " + response);
        } catch (Exception e){
            e.printStackTrace();
        }

        return response;
     }

Services is the way to go. REST (I recommend this one on Android), or SOAP based. There're loads of tutorials on getting an android app communicate a service, even with .net / wcf ones.

Tho you can always just open raw sockets and send data with some custom protocol.

Edit:

Here's the doInBackground part of my asynctask handling http post communication, maybe that'll help:

protected String doInBackground(String... req) {

        Log.d(TAG, "Message to send: "+req[0]);
        HttpPost p = new HttpPost(url);

        try{
            p.setEntity(new StringEntity(req[0], "UTF8"));
        }catch(Exception e){
            e.printStackTrace();
        }
        p.setHeader("Content-type", "application/json");

        String response = "";
        try{
            HttpResponse resp = hc.execute(p, localContext);
            InputStream is = resp.getEntity().getContent();
            response = convertStreamToString(is);
            Log.d("Response", "Response is " + response);
        } catch (Exception e){
            e.printStackTrace();
        }

        return response;
     }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文