【问题标题】:How to get data from Server using ID in Android如何在 Android 中使用 ID 从服务器获取数据
【发布时间】:2013-08-23 07:24:18
【问题描述】:

我正在编写一个原生 Android 应用程序,我在其中使用 PHP MYSQL 从服务器获取数据

在此 [约会列表] 中,我允许用户重新安排约会,但每当我点击项目时会出现空白表单,简而言之,不会获取我在列表中单击的特定约会的数据。

问题

如何使用 AppointmentID 在表单中显示数据?

下面我展示了我编写的所有必需的代码[客户端和服务器端都]

UpcomingActivity.java:

        @Override
        public boolean onContextItemSelected(MenuItem item) {
            AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
            int menuItemIndex = item.getItemId();
            String[] menuItems = Cmd;
            String CmdName = menuItems[menuItemIndex];
            // Check Event Command
            if ("Cancel".equals(CmdName)) 
            {
                Toast.makeText(UpcomingActivity.this,"Selected Cancel",Toast.LENGTH_LONG).show();           
            }
            else if ("Reschedule".equals(CmdName)) 
            {
                Toast.makeText(UpcomingActivity.this,"Selected Update",Toast.LENGTH_LONG).show();

                String sAppointmentID = MyArrList.get(info.position).get("UserID").toString();                      
                Log.d(tag, "sAppointmentID :: " + sAppointmentID);

                Intent newActivity = new Intent(UpcomingActivity.this, UpdateActivity.class);
                newActivity.putExtra("UserID", sAppointmentID);                     
                startActivity(newActivity);
            }             
            return true;
        }

UpdateActivity.java:

            public void showInfo()
            {
                final TextView tAppointmentID = (TextView)findViewById(R.id.txtUsername);
                final TextView tType = (TextView)findViewById(R.id.txtName);
                final TextView tDate = (TextView)findViewById(R.id.txtEmail);
                final TextView tTime = (TextView)findViewById(R.id.txtTel);

                Button btnSave = (Button) findViewById(R.id.btnSave);
                Button btnCancel = (Button) findViewById(R.id.btnCancel);
                String url = "http://10.0.2.2/appointments/getByMemberID.php";

                Intent intent= getIntent();
                final String AppointmentID = intent.getStringExtra("AppointmentID");
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("sAppointmentID", AppointmentID));

                String resultServer  = getHttpPost(url,params);
                String strAppointmentID = "";               
                String strType = "";
                String strDate = "";
                String strTime = "";

                JSONObject c;
                try {
                    c = new JSONObject(resultServer);

                    strAppointmentID = c.getString("UserID");
                    Log.d(TAG, "String strAppointmentID" + strAppointmentID);
                    strType = c.getString("Type");
                    Log.d(TAG, "String strType" + strType);
                    strDate = c.getString("Date");
                    Log.d(TAG, "String strDate" + strDate);
                    strTime = c.getString("Time");
                    Log.d(TAG, "String strTime" + strTime);

                    if(!strAppointmentID.equals(""))
                    {
                        tAppointmentID.setText(strAppointmentID);
                        tType.setText(strType);
                        tDate.setText(strDate);
                        tTime.setText(strTime);
                    }   
                    else
                    {
                        tAppointmentID.setText("-");
                        tType.setText("-");
                        tDate.setText("-");
                        tTime.setText("-"); 
                        btnSave.setEnabled(false);
                        btnCancel.requestFocus();
                    }

                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }   
            }

【问题讨论】:

  • 先确认php没问题。为此,请在 getByMemberID.php 中手动输入 id 并从 Web 浏览器加载它。 10.0.2.2/appointments/getByMemberID.php
  • 请注意SQL注入攻击。您当前的代码是完全开放的,因为您直接在查询中输入用户输入 ($_POST)。谷歌一下——如果你使用 PHP 的 mysqli 包(或mysql 的其他替代品),或者在使用之前对输入进行一些检查,它会很快修复。

标签: php android mysql


【解决方案1】:
  • 选项 1

    开发 Java EE Web 服务,并将其托管在服务器上。您的 Android 应用需要将数据与网络服务相关联并将其发送到服务器。服务器可以接收数据并做它想做的事。服务器也可以通过webservice的相同响应来处理和响应一些更多的信息。你需要在谷歌上搜索 hello world Java EE。我找到了一个。

    一旦您的 Java EE 在本地桌面(或开发机器)上运行,您可以将 WAR 或 EAR 复制到 EC2 以使应用程序在 Internet 上运行,而无需经历创建网络的痛苦已打开且处于 DMZ。

  • 选项 2

    您可以开发一个基于 java 套接字的产品,您的 android 应用程序通过 clientsocket 连接到该产品。通过这种方式,您可以序列化数据并通过流发送。并在服务器端反序列化并重建对象。这种方法需要更加规范的软件开发。如果您是第一次开发人员,这可能会非常麻烦,我会推荐选项 1。

希望我已经给出了一些基本的方向。

【讨论】:

    【解决方案2】:
    1. 首先确保您拥有有效的AppointmentIDUserIdand

    2. UpcomingActivity.java onContextItemSelected 方法中,您没有提供AppointmentID,而是仅向Intend 提供UserID

    3. 列出但在Updateactivity showData 方法中,您请求的intent.getStringExtra("AppointmentID") 无效。

    所以你更新你的UpcomingActivity.java 应该是这样的

     public boolean onContextItemSelected(MenuItem item) {
            AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
            int menuItemIndex = item.getItemId();
            String[] menuItems = Cmd;
            String CmdName = menuItems[menuItemIndex];
            // Check Event Command
            if ("Cancel".equals(CmdName)) 
            {
                Toast.makeText(UpcomingActivity.this,"Selected Cancel",Toast.LENGTH_LONG).show();           
            }
            else if ("Reschedule".equals(CmdName)) 
            {
                Toast.makeText(UpcomingActivity.this,"Selected Update",Toast.LENGTH_LONG).show();
    
                String sAppointmentID = MyArrList.get(info.position).get("UserID").toString();                      
                Log.d(tag, "sAppointmentID :: " + sAppointmentID);
    
                Intent newActivity = new Intent(UpcomingActivity.this, UpdateActivity.class);
                newActivity.putExtra("UserID", sAppointmentID);
                newActivity.putExtra("AppointmentID", MyArrList.get(info.position).get("AppointmentID").toString());// <== here                     
                startActivity(newActivity);
            }             
            return true;
        }
    

    【讨论】:

    【解决方案3】:

    我注意到了几个问题(可能是拼写错误,但我还是更愿意问):

    • 您正在调用getByMemberID.php,但您没有在问题中包含其源代码 - 您确实有这样的服务,对吧? :)
    • onContextItemSelected 中,您正在启动UpdateActivity 并通过UserID 作为额外。但在该活动的 showInfo 方法中,您试图从 Intent 的额外内容中获取 AppointmentID,因此它不处理任何数据。

    【讨论】:

      【解决方案4】:

      1) 试试你的 PHP 代码是否正常工作... 您可以通过直接在服务器上运行并通过 UserId 和 appoinmentId 作为参数传递来做到这一点?

      例如.... www.abc.com?sUserId=123?sAppointmentID=456

      查看天气,它会显示正确的输出。

      2)您正在调用 getByMemberID.php,但您已指定它的代码,但您没有传递“AppointmentID”,还请检查您是否正在检索它的 php 代码天气??

      3) 在 UpdateActivity.java 中调用 updateData.php:您没有提供“AppointmentID”,而是在 php 代码中检索它,默认情况下它将获得空值,这将是一个错误

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-30
        • 2013-03-18
        • 1970-01-01
        • 2019-12-12
        • 1970-01-01
        相关资源
        最近更新 更多