将android活动变量发送到php脚本

时间:2016-02-08 18:07:08

标签: php android android-activity

我试图通过QR码读取PHP脚本URL,然后检索设备的MAC,之后,我想将MAC发送到php脚本,但是如果我尝试的话,android app会崩溃。

我还使用 intent 在浏览器中打开网址。我应该在php端写什么来检索MAC地址和android活动的任何变化?

代码

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_decoder);

    mydecoderview = (QRCodeReaderView) findViewById(R.id.qrdecoderview);
    mydecoderview.setOnQRCodeReadListener(this);

    myTextView = (TextView) findViewById(R.id.exampleTextView);

    line_image = (ImageView) findViewById(R.id.red_line_image);

    TranslateAnimation mAnimation = new TranslateAnimation(
            TranslateAnimation.ABSOLUTE, 0f,
            TranslateAnimation.ABSOLUTE, 0f,
            TranslateAnimation.RELATIVE_TO_PARENT, 0f,
            TranslateAnimation.RELATIVE_TO_PARENT, 0.5f);
    mAnimation.setDuration(1000);
    mAnimation.setRepeatCount(-1);
    mAnimation.setRepeatMode(Animation.REVERSE);
    mAnimation.setInterpolator(new LinearInterpolator());
    line_image.setAnimation(mAnimation);

     Button button2 = (Button) findViewById(R.id.button2);
    button2.setOnClickListener(
            new View.OnClickListener() {
                public void onClick(View v) {
               /*String ur="http://192.168.0.105/project/rec.html";
                    Intent intent1=new Intent("android.intent.action.VIEW");
                    if (!ur.contains("http://"))
                        ur = "http://" + ur;
                    intent1.setData(Uri.parse(ur));

                    startActivity(intent1);// Perform action on click*/



                }

            });




}

// Called when a QR is decoded
// "text" : the text encoded in QR
// "points" : points where QR control points are placed

@Override
public void onQRCodeRead(String text, PointF[] points) {

    WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    WifiInfo wInfo = wifiManager.getConnectionInfo();
    action = wInfo.getMacAddress();

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(addr);


    try {

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair(1);
        nameValuePairs.add(new BasicNameValuePair("action",action));

        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));


        ResponseHandler<String> responseHandler = newBasicResponseHandler();
        String response = httpclient.execute(httppost, responseHandler);


        String reverseString = response;


    } catch (ClientProtocolException e) {


    } catch (IOException e) {




    }

    Intent localIntent = new Intent("android.intent.action.VIEW");
    if (!text.contains("http://"))
         text = "http://" + text;
    addr=text;
    localIntent.setData(Uri.parse(text));

    startActivity(localIntent);


}

1 个答案:

答案 0 :(得分:0)

error_reporting(0);
include("db_config.php");
$response = array();
if (isset($_GET['id'])) {
    $id = $_GET['id'];
    $item = $_GET['item']; 
    $r = mysql_query("select Department_id from student where st_id = '$id'");
    $row = mysql_fetch_array($r);
    $Department_id = $row[0];
    $r = mysql_query("select Subject_name from subject where Subject_code ='$item'");
    $row = mysql_fetch_array($r);
    $Subject_name = $row[0];
    $result = mysql_query("INSERT INTO `sis_fyp`.`student-subject` (`st_id`, `Subject_code`, `Status`, `grade`, `Department_id`, `Subject_name`) VALUES ('$id', '$item', 'Repeat', 'W', '$Department_id', '$Subject_name');");

    if ($result>0) {
        $response["success"] = 1;
    } else {
        $response["success"] = 0;
    }
    // echoing JSON response
    echo json_encode($response);
}

只需制作上面的php脚本即可 并传递你的网址和android如:

wwww.yoururl.com?id=$id&item=$item