2013-02-10 1 views
1

URL이있는 NFC 태그를 검색하는 NFC 기반 앱을 작성하고 있습니다. 일단 태그가 스캔되면 앱은 데이터베이스에서 정보를 검색하여 ListView에 표시해야합니다.NDEF URI 페이로드의 문자가 잘못되었습니다.

NFC 태그를 스캔 할 때 오류가 발생합니다.

Error in http connection java.lang.IllegalArgumentException: Illegal character in scheme at index 0: ??http://abc.com/~090123/get_items.php 

로그 캣은 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; 
} 

나는 t와 같은 이전 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> 

어떤 도움을 주시면 감사하겠습니다.

답변

3

URI 페이로드의 첫번째 바이트가 URI 식별자 코드, 그것은 http://www. 또는 mailto: 같은 URI 접두사이다. 페이로드 길이에 바이트를 올바르게 추가했으며 URL을 복사 할 때 1 오프셋을 사용했습니다. 0x00 (프리픽스가 없음)이되도록이 바이트를 쓰지 마십시오. 즉, 전체 URL을 작성해야합니다. 몇 바이트를 저장하려는 경우 http:// 접두어로 0x03으로 설정할 수 있지만 괜찮습니다.

msg.getRecords()[0].getPayload()으로 페이로드를 읽는 경우 해당 바이트를 잘라내어 접두사를 직접 추가해야합니다. 내 생각 엔 네이티브 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은 식별자 코드에 대한 접두사 문자열을 반환하는 간단한 스위치 문을 포함 할 수 있습니다.

현재 코드의 목록을 볼 수 있습니다 : 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:[email protected] 
0x08  ftp://ftp. 
0x09  ftps:// 
0x0A  sftp:// 
... 
+0

안녕하세요,이 같은 URI 식별자 코드를 추가하는 시도했다 : 바이트 [] 페이로드 = 새로운 바이트 [uriField합니다. 길이 +1]; 페이로드 [0] = 0x03; System.arraycopy (uriField, 0, payload, 1, uriField.길이); 여전히 같은 오류가 발생합니다. – Sujal

+0

@Sujal 요점은 식별자 코드 바이트가 페이로드에 읽을 때 포함되어 있다는 것이 었습니다. 예를 들어 대답을 업데이트했습니다. – Kapep

+0

고마워요! 이것은 문제를 해결했습니다. – Sujal

0

NDEF 레코드를 잘못 구문 분석하고 있습니다. 첫 번째 바이트는 문자가 아닙니다. NFC 포럼 표준을 읽거나이 상황을 돕기 위해 만든 lib을 확인하십시오. Android 소스 코드를 확인하여 읽으려는 레코드가 어떻게 구성되는지 확인할 수 있습니다.

관련 문제