Java 从我的android应用程序zxing向我的在线MySQL数据库发送数据
我正在使用Zxing框架构建一个简单的二维码阅读器,到目前为止,它运行良好。这里的问题是,我没有找到捕获的二维码数据的变量。如何将捕获的二维码数据发送到在线MySQL数据库 下面是我的代码 Main_activity.javaJava 从我的android应用程序zxing向我的在线MySQL数据库发送数据,java,android,mysql,regex,Java,Android,Mysql,Regex,我正在使用Zxing框架构建一个简单的二维码阅读器,到目前为止,它运行良好。这里的问题是,我没有找到捕获的二维码数据的变量。如何将捕获的二维码数据发送到在线MySQL数据库 下面是我的代码 Main_activity.java package io.github.akodiaemmanuel.mapenziscanner; import android.app.Activity; import android.app.AlertDialog; import android.os.Bun
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);
}
}
您可以将数据发送到PHP脚本,该脚本将在MySQL数据库中保存这些数据: 要从Android向PHP发送数据,可以使用 在这里,我们将调用页面url_to_my_script.php并在POST中发送数据。当页面被加载(未完成)时,我们得到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
}
}
}
});
Ion.with(上下文)
.load(“url_to_my_script.php”)
.setBodyParameter(“qrdata”,qrdata)
.asJsonObject()
.withResponse()
.setCallback(新的FutureCallback(){
@凌驾
未完成公共无效(例外情况e,响应){
Log.i(“myactivity”,“jsonresult为”+响应);
//如果我们有错误或代码
if(e!=null | | response.getHeaders().code()!=200){
Log.e(“myactivity”、“错误无效请求”+e);
Log.e(“myactivity”,“响应代码:”+reponse.getHeaders().code());
}否则{
如果(!success.equals(“1”)){
String error=result.get(“error_message”).toString();
}否则{
//一切都好,做点什么吧
}
}
}
});
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"}';
}
?>
您可以将数据发送到PHP脚本,该脚本将在MySQL数据库中保存这些数据:
要从Android向PHP发送数据,可以使用
在这里,我们将调用页面url_to_my_script.php并在POST中发送数据。当页面被加载(未完成)时,我们得到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
}
}
}
});
Ion.with(上下文)
.load(“url_to_my_script.php”)
.setBodyParameter(“qrdata”,qrdata)
.asJsonObject()
.withResponse()
.setCallback(新的FutureCallback(){
@凌驾
未完成公共无效(例外情况e,响应){
Log.i(“myactivity”,“jsonresult为”+响应);
//如果我们有错误或代码
if(e!=null | | response.getHeaders().code()!=200){
Log.e(“myactivity”、“错误无效请求”+e);
Log.e(“myactivity”,“响应代码:”+reponse.getHeaders().code());
}否则{
如果(!success.equals(“1”)){
String error=result.get(“error_message”).toString();
}否则{
//一切都好,做点什么吧
}
}
}
});
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"}';
}
?>
您面临的问题是什么?您的问题中的PHP、mysql代码在哪里?您可以将数据发送到PHP页面,该页面将这些数据保存在mysql数据库中。您的代码中没有PHP,问题是unclear@Ludo如何将数据发送到PHP页面。事实上,我喜欢这个主意。但安卓应用程序中是否有php文件?你面临的问题是什么?你的问题中的php、mysql代码在哪里?你可以将数据发送到php页面,将这些数据保存在mysql数据库中。你的代码中没有php,问题是unclear@Ludo如何将数据发送到PHP页面。事实上,我喜欢这个主意。但是安卓应用程序中可以有php文件吗