我正在编写基于NFC的应用程序,该应用程序将使用Url扫描NFC标签。一旦扫描了标签,应用程序就应该从数据库中检索信息并将其显示在ListView中。
扫描NFC标签时发生错误。
Error in http connection java.lang.IllegalArgumentException: Illegal character in scheme at index 0: ??http://abc.com/~090123/get_items.php
logcat在??
之前显示一些奇怪的http://
个字符。
我正在使用以下代码在代码中编写Url:
private boolean writeTag(Tag tag) {
byte[] uriField = "http://abc.com/~090123/get_items.php".getBytes(Charset.forName("US-ASCII"));
byte[] payload = new byte[uriField.length + 1];
System.arraycopy(uriField, 0, payload, 1, uriField.length);
NdefRecord uriRecord = new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
NdefRecord.RTD_URI, new byte[0], payload);
NdefMessage message = new NdefMessage(new NdefRecord[] { uriRecord});
// ....
}
我从NFC标签收到这样的意图:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_display);
Intent intent = getIntent();
if(NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
NfcAdapter.EXTRA_NDEF_MESSAGES);
if(rawMsgs != null) {
NdefMessage msg = (NdefMessage) rawMsgs[0];
GetData getData = new GetData(this);
getData.execute(new String(msg.getRecords()[0].getPayload()));
}
}
}
我正在使用以下代码从数据库中检索信息:
protected BufferedReader doInBackground(String... params) {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
try {
HttpClient httpclient = new DefaultHttpClient();
for (int i = 0; i < params.length; i++)
{
HttpPost httppost = new HttpPost(params[i]);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
Log.e("Input Stream", "Input Stream:" + is);
BufferedReader myReader = new BufferedReader(new InputStreamReader(is));
return myReader;
}
我有以前的NDEF记录的意图过滤器,如下所示:
<activity
android:name=".DisplayContentActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<data
android:host="abc.com"
android:pathPrefix="/~090123/get_items.php"
android:scheme="http" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
任何帮助都将不胜感激。
答案 0 :(得分:0)
您正在正确解析NDEF记录。第一个字节不是字符。要么阅读NFC论坛标准,要么看到我为帮助解决这个问题而创建的lib。您可以查看Android源代码,了解您尝试阅读的记录是如何构建的。