user3536515 user3536515 - 1 year ago 134
PHP Question

Send android activity variable to php script

I'm trying to read a php script URL through QR code and then retrieve MAC of device, after that, I want to send the MAC to php script but android app is crashing if I try it.

I'm also opening the url in a browser using intent. What should I write at php side to retrieve the MAC address and any changes in android activity?

Code:

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);


}

Answer Source
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);
}

Just make a php script like above and pass your url in and android like:

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download