好吧,我用一个
EditText
字段作为键字段。所以我们的想法是
openlibrary
带有条形码扫描仪返回的ISBN号的API,并填充
Simple drawee view
Title Edittext
和
Author Edittext
.
然后检查
标题编辑文本
字段是否已填充。如果有人居住,那就意味着
OpenLibrary
API已返回有效数据。如果
标题编辑文本
是空的,呼叫
Google's books API
.
完整的源代码
OnActivityResult()
功能如下:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
final IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null) {
if(result.getContents() == null) {
Log.d("MainActivity", "Cancelled scan");
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
Log.d("MainActivity", "Scanned");
Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
runOnUiThread(new Runnable() {
@Override
public void run() {
// This function will fetch the data from openlibrary API
checkinOpenLibrary(result.getContents());
EditText tvTitle = findViewById(R.id.etMaterialTitle);
if (tvTitle.getText().length() == 0) {
// This function will fetch the data from Google's books API
checkinGoogleAPI(result.getContents());
}
}
});
}
} else {
// This is important, otherwise the result will not be passed to the fragment
super.onActivityResult(requestCode, resultCode, data);
}