java - NFC标签和php

标签 java android nfc

我对 NFC 标签不熟悉,对其工作原理很感兴趣。我购买了 NFC 标签,并且能够将学生 ID 写入标签。现在我的问题是如何将学生 ID 传递到 php Web 服务,并在通过我的应用程序扫描学生卡时检查该学生是否已支付餐费,然后才能前往自助餐厅。

请任何人帮助我如何做到这一点。以下是我所做的。

//reading the tag
private String readText(NdefRecord record) throws UnsupportedEncodingException {

        byte[] payload = record.getPayload();

        // Get the Text Encoding
        String textEncoding = ((payload[0] & 128) == 0) ? "UTF-8" : "UTF-16";

        // Get the Language Code
        int languageCodeLength = payload[0] & 0063;

        // Get the Text
        return new String(payload, languageCodeLength + 1, payload.length - languageCodeLength - 1, textEncoding);
    }

    @Override
    protected void onPostExecute(String result) {
        if (result != null) {
        //show the student id in a textedit
            mTextView.setText(result);

            //pass variable to the server and get contents. I want to pass the student id to the method
            getstudentinfo(result);
        }
    }

      void getstudentinfo(String studID) {

        //get connection to the server using http request
        httpclient=new DefaultHttpClient();

        httppost= new HttpPost("http://myip/getStudentBl.php?studID="+ studID);

        try{

        response = getThreadSafeClient().execute(httppost);
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        final String response = httpclient.execute(httppost, responseHandler);

        //checking the response and info the user
        runOnUiThread(new Runnable() {
            public void run() {

            dialog.dismiss();
            }
            });

        //if the user is found
        if(response.equalsIgnoreCase("Student Found")){
            runOnUiThread(new Runnable() {
            public void run() {
            //Toast.makeText(MainActivity.this,"Saved Successfull", Toast.LENGTH_SHORT).show();
                stdBalance.setText("Student Balance " + response);
            }
            });
            //show the dashboard screen
            startActivity(new Intent(MainActivity.this, MainActivity.class));
        }else if(response.equalsIgnoreCase("No Record")){
            //show error results
            showAlert();
        }
        //end try catch
        }catch(Exception e){
            dialog.dismiss();
            System.out.println("Exception : " + e.getMessage());
            }

    }

最佳答案

根据我的理解,至少对于 Android 阅读器来说,如果标签包含 URL,它将自动加载浏览器并转到该 URL(不会询问您是否要打开应用程序,也不会询问您是否要访问网址)。您应该能够将 Student_id 作为查询字符串并在页面中使用它。

关于java - NFC标签和php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18846228/

相关文章:

android - 在不同设备上运行 NFC 应用会导致解析 EXTRA_NDEF_MESSAGE 不一致

java - 使用 Jetty Content Exchange 和 SSL 时是什么原因导致 "broken pipe"?

Android Facebook SDK 3.0 登录错误 - 发生错误。请稍后再试

java - 将属性附加到 JAR 的 manifest.mf

android - scrollView 直到结束才滚动

Android ViewPager 刷新 fragment

android - 在 Android 上通过 NFC 读取 Visa payWave 信用卡详细信息

Android 光束 Intent 过滤器

jakarta-ee - Java EE 应用程序中的小程序访问蓝牙外设客户端

java - 类加载与类初始化