【问题标题】:Reading Data Sent by Android Client using Servlet使用 Servlet 读取 Android 客户端发送的数据
【发布时间】:2011-08-16 22:26:11
【问题描述】:

我正在从安卓客户端向服务器发送数据

httpPost.setEntity("Some String");

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

我想检索此字符串或名称值对或哈希图。

我想使用 servlet 而不是 php 脚本。我查看了 HttpRequest 的所有方法,但遗漏了一些东西。

我正在模拟器中运行应用程序,该应用程序带有一个 tomcat 本地主机和来自模拟器的 10.0.2.2 ip。

请提供此类 servlet 的示例。提前致谢

【问题讨论】:

    标签: android servlets


    【解决方案1】:

    在您的 servlet 调用 request.getParameter("parameterName") 中检索从您的 Android 设备发送的参数。参数名称与您在名称/值对中使用的名称相同。

    假设您使用的是 POST(如果没有,请改写 doGet),您的 servlet 应该如下所示:

    public class MyServlet extends HttpServlet {
      public void doPost(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
    
          String value = request.getParameter("parameterName");
    
          ... more
      }
    }
    

    【讨论】:

    • 放一个断点来查看你正在接收的数据。如果该参数不存在,则表明您的应用的 Android 端存在问题。
    猜你喜欢
    • 2011-07-22
    • 2012-07-17
    • 2013-04-29
    • 1970-01-01
    • 2018-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-17
    相关资源
    最近更新 更多