我正在使用Zxing框架构建一个简单的QR码阅读器,到目前为止它工作正常。这里的问题是我未能找到捕获的QR码数据的变量。如何将捕获的QR码数据发送到我的在线MySQL数据库。
以下是我的代码
Main_activity.java
package io.github.akodiaemmanuel.mapenziscanner;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import com.google.zxing.Result;
import me.dm7.barcodescanner.core.ViewFinderView;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
/**
* Created by Akodia Emmanuel on 9/18/2017.
*/
public class MainActivity extends Activity implements ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerview;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
}
public void onClick(View v){
mScannerview = new ZXingScannerView(this);
setContentView(mScannerview);
mScannerview.setResultHandler(this);
mScannerview.startCamera();
}
@Override
public void handleResult(Result result) {
//Do anything with the result here
Log.w("handleResult",result.getText());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Scan Result");
builder.setMessage(result.getText());
AlertDialog alertDialog = builder.create();
alertDialog.show();
//resume scanning
//mScannerview.resumeCameraPreview(this);
}
}
答案 0 :(得分:0)
您可以将数据发送到将在MySQL数据库中保存这些数据的PHP脚本:
要从Android向PHP发送数据,您可以使用https://github.com/koush/ion
这里我们将调用页面url_to_my_script.php并在POST中发送qrdata。当页面加载(onCompleted)时,我们得到JSON响应并根据响应做一些事情。
机器人:
Ion.with(context)
.load("url_to_my_script.php")
.setBodyParameter("qrdata", qrdata)
.asJsonObject()
.withResponse()
.setCallback(new FutureCallback<Response<JsonObject>>() {
@Override
public void onCompleted(Exception e, Response<JsonObject> reponse) {
Log.i("myactivity", "jsonresult is " + reponse);
//if we have an error or code
if (e != null || reponse.getHeaders().code() != 200) {
Log.e("myactivity", "Error invalid request" + e);
Log.e("myactivity", "response code:"+reponse.getHeaders().code());
} else {
if (!success.equals("1")) {
String error = result.get("error_message").toString();
}else{
//everything is ok do something
}
}
}
});
Php脚本:
这是PHP脚本获取数据并返回JSON响应
<?php
header('Content-type: application/json');
require_once 'database_config.php';
function insertMyData($data){
//do something
// if ok return true else false
}
if($_POST){
$qrdata= trim($_POST['qrdata']);
if(insertMyData($qrdata)){
echo '{"success":1}';
}else{
echo '{"success":0,"error_message":"Put a message her"}';
}
} else {
echo '{"success":0,"error_message":"Invalid date"}';
}
?>