gpt4 book ai didi

android - NDEF URI 负载中的非法字符

转载 作者:行者123 更新时间:2023-11-29 18:03:07 26 4
gpt4 key购买 nike

我正在编写基于 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 标签的 Intent ,如下所示:

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 记录的 Intent 过滤器:

<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>

如有任何帮助,我们将不胜感激。

最佳答案

URI 负载的第一个字节是URI 标识符代码,它是一个URI 前缀,如http://www.mailto:。您正确地向有效负载长度添加了一个字节,并在复制 URL 时使用了 1 的偏移量。您永远不会写入此字节,因此它仍然是 0x00(无前缀),这意味着您需要写入整个 URL。这很好,但如果您想节省几个字节,您可以将它设置为 0x03 作为 http:// 前缀。

当使用msg.getRecords()[0].getPayload() 读取有效负载时,您需要切断该字节并自行添加前缀。我认为没有用于此的 native Android API,因此您必须自己查找前缀或尝试其他答案中提到的库。

要正确读取 uri,您应该使用如下内容:

byte[] payload = msg.getRecords()[0].getPayload();
byte identifierCode = payload[0];

String prefix = getUrlPrefix(identifierCode); // you need to implement this one
String url = prefix +
new String(payload, 1, payload.length -1, Charset.forName("US-ASCII"));

getData.execute(url);

getUrlPrefix 方法可以包含一个简单的 switch 语句来返回标识符代码的前缀字符串。

您可以在此处查看代码列表:About the NDEF Format

Value    Protocol
----- --------
0x00 No prepending is done ... the entire URI is contained in the URI Field
0x01 http://www.
0x02 https://www.
0x03 http://
0x04 https://
0x05 tel:
0x06 mailto:
0x07 ftp://anonymous:anonymous@
0x08 ftp://ftp.
0x09 ftps://
0x0A sftp://
...

关于android - NDEF URI 负载中的非法字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14798731/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com