我试图检查扫描的qr代码是否与我的一个sql帖子相同。 如果与我的一个sql帖子匹配,则此帖子在接下来的4天内不可用。我尝试了以下检查这个qr值:
public class Konto extends AppCompatActivity implements ZXingScannerView.ResultHandler {
private TextView txtName;
private ImageButton btnLogout;
private String TAG = Locations.class.getSimpleName();
private ProgressDialog pDialog;
String qr_result;
// URL to get contacts JSON
private static String url = "yxxx";
ArrayList<HashMap<String, String>> contactList;
private SQLiteHandler db;
private SessionManager session;
private ZXingScannerView mScannerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_konto
);
new GetContacts().execute();
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
alertBuilder.setCancelable(false);
alertBuilder.setTitle("Permission necessary");
alertBuilder.setMessage("Fine Location is necessary");
alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(Konto.this, new String[]{Manifest.permission.CAMERA}, 1234);
}
});
AlertDialog alert = alertBuilder.create();
alert.show();
} else {
ActivityCompat.requestPermissions(Konto.this, new String[]{Manifest.permission.CAMERA}, 1234);
}
}
ImageButton test = (ImageButton) findViewById(R.id.testforsql);
test.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mScannerView = new ZXingScannerView(Konto.this);
setContentView(mScannerView);
mScannerView.setResultHandler(Konto.this);
mScannerView.startCamera();
}
});
txtName = (TextView) findViewById(R.id.kontoname);
btnLogout = (ImageButton) findViewById(R.id.logoutbtn);
// SqLite database handler
db = new SQLiteHandler(getApplicationContext());
// session manager
session = new SessionManager(getApplicationContext());
if (!session.isLoggedIn()) {
logoutUser();
}
// Fetching user details from sqlite
HashMap<String, String> user = db.getUserDetails();
String name = user.get("name");
String email = user.get("email");
// Displaying the user details on the screen
txtName.setText(name);
// Logout button click event
btnLogout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
logoutUser();
}
});
contactList = new ArrayList<>();
}
@Override
public void handleResult(Result result) {
Log.w("handleResult", result.getText());
AlertDialog.Builder builder = new AlertDialog.Builder(Konto.this);
builder.setTitle("Scan Result");
builder.setMessage(result.getText());
AlertDialog alertDialog = builder.create();
alertDialog.show();
qr_result = result.getText();
mScannerView.resumeCameraPreview(Konto.this);
}
/**
* Logging out the user. Will set isLoggedIn flag to false in shared
* preferences Clears the user data from sqlite users table
*/
private void logoutUser() {
session.setLogin(false);
db.deleteUsers();
// Launching the login activity
Intent intent = new Intent(Konto.this, LoginActivity.class);
startActivity(intent);
finish();
}
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(Konto.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONArray contacts = new JSONArray(jsonStr);
// Getting JSON Array node
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String id = c.getString("id");
String name = c.getString("name");
String street = c.getString("street");
String postalcode = c.getString("postalcode");
String entry = c.getString("entry");
String opening = c.getString("opening");
String agegroup = c.getString("agegroup");
String imageurl = c.getString("imageurl");
String urlbtn = c.getString("urlbtn");
String Fsk = c.getString("Fsk");
String city = c.getString("city");
String lat = c.getString("lat");
String lng = c.getString("lng");
String bg = c.getString("background");
String qr = c.getString("qr");
HashMap<String, String> contact = new HashMap<>();
// adding each child node to HashMap key => value
contact.put("id", id);
contact.put("name", name);
contact.put("street", street);
contact.put("postalcode", postalcode);
contact.put("entry", entry);
contact.put("opening", opening);
contact.put("agegroup", agegroup);
contact.put("imageurl", imageurl);
contact.put("urlbtn", urlbtn);
contact.put("Fsk", Fsk);
contact.put("city", city);
contact.put("lat", lat);
contact.put("lng", lng);
contact.put("background", bg);
contact.put("qr",qr);
// adding contact to contact list
contactList.add(contact);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
for (final HashMap<String, String> contact : contactList){
contact.get("qr");
if (qr_result.equals(contact.get("qr"))){
Toast.makeText(getApplicationContext(),"QR Accepted",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(),"NOt goog",Toast.LENGTH_LONG).show();
}
}
}
}
}