将 Android 应用程序连接到 restful 网络服务

Connect Android App to restful webservice

我知道这个问题已经被问过很多次了,但我找不到合适的答案,希望你们中的一个能帮助我;-)

我正在尝试使用智能手机设备上的 android 应用程序与休息网络服务进行通信(因此没有模拟器)。 Web 服务是 windows 机器上 192.168.0.2:8080 上的 运行。 该设备连接在与 pc 相同的 wlan 网络中。 当我使用设备的 chrome 浏览器打开 link 时,网络服务正常响应。

这是我用来在 AsyncTask 中连接到网络服务的代码:

    protected Integer doInBackground(String... params)
    {
        try
        {
            AndroidHttpClient client = AndroidHttpClient.newInstance("androidClient", getContext());
            HttpPost post = new HttpPost(params[0]);
            String xml = new ScoreDto(new Random(System.currentTimeMillis()).nextLong()).toXml();
            post.setEntity(new StringEntity(xml));
            return client.execute(post).getStatusLine().getStatusCode();
        }
        catch (Exception e)
        {
            Log.e(TAG, e.getLocalizedMessage());
            return 0;
        }
    }

有什么想法吗?问题似乎与本地网络有关.. 如果我想通过我的 android 设备连接到外部服务器,是否需要特殊限制或权限? 提前致谢;-)

感谢所有post发表评论试图提供帮助的人。
我弄清楚出了什么问题:
post 请求缺少 content-type header...

功能齐全的 doBackground- 实现如下所示:

protected Integer doInBackground(String... params)
{
    try
    {
        //TODO check if params.length >=2 & not empty
        AndroidHttpClient client = AndroidHttpClient.newInstance("androidClient", getContext());
        HttpPost post = new HttpPost(params[0]);
        post.setHeader("content-type", "application/xml");
        post.setEntity(new StringEntity(params[1]));
        return client.execute(post).getStatusLine().getStatusCode();
    }
    catch (Exception e)
    {
        Log.e(TAG, e.getLocalizedMessage());
        return 0;
    }
}