0

簡単に説明しますが、不明な点があれば質問してください。vk.com (誰かが知らない場合に備えて大規模なソーシャル ネットワーク) からユーザーのオーディオ リストを取得しています。応答は次のようになります。

   {"response":[{
  "aid":"60830458","owner_id":"6492","artist":"Noname","title":"Bosco",
  "duration":"195","url":"http:\/\/cs40.vkontakte.ru\/u06492\/audio\/2ce49d2b88.mp3"},
  {"aid":"59317035","owner_id":"6492","artist":"Mestre Barrao","title":"Sinhazinha",
  "duration":"234","url":"http:\/\/cs510.vkontakte.ru\/u2082836\/audio\/
  d100f76cb84e.mp3"}]}

ユーザーは自分のプロファイルに数百または数千ものトラックを持つことができるため、通常はもっと長くなります。アーティストとタイトルにもキリル文字を含めることができるため、パーサーで UTF-8 を使用しました。私はJSONにあまり詳しくありません。次を使用して応答を解析しようとしています:

 public class JSONParser {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";


public JSONParser() {

}

public static JSONObject getJSONFromUrl(String url) {


    try {

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();          

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "UTF-8"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }


    return jObj;

}
  }

しかし、アプリは IllegalArgumentException 例外 (インデックス 0 のスキームに不正な文字) でクラッシュします。

02-27 10:37:35.870: E/AndroidRuntime(21038): FATAL EXCEPTION: main
02-27 10:37:35.870: E/AndroidRuntime(21038): java.lang.RuntimeException: Unable to resume activity {com.vadim.android.vk_player/com.vadim.android.vk_player.MainActivity}: java.lang.IllegalArgumentException: Illegal character in scheme at index 0: {"response":[{"aid":191819427,"owner_id":13590837,"artist":"Buena Vista Social Club","title":"El Cuarto de Tula","duration":445,"url":"http:\/\/cs548.userapi.com\/u361189\/audios\/b8c6a3bdb0bb.mp3","lyrics_id":"1133390"},{"aid":191477921,"owner_id":13590837,"artist":"Buena Vista Social Club","title":"Hasta Siempre Comandante Che Guevara","duration":193,"url":"http:\/\/cs4515.userapi.com\/u7198823\/audios\/5fafa2136e16.mp3","lyrics_id":"2876258"},{"aid":190900891,"owner_id":13590837,"artist":"Slade","title":"Oh la la in L.A.","duration":229,"url":"http:\/\/cs4962.userapi.com\/u9811745\/audios\/ed7445d38bef.mp3"},{"aid":188976833,"owner_id":13590837,"artist":"PR-MEX","title":"У Билли Гейтса","duration":126,"url":"http:\/\/cs5002.userapi.com\/u4693819\/audios\/a1899ebb7716.mp3","lyrics_id":"5201762"},{"aid":186998450,"owner_id":13590837,"artist":"The Best Latino Dance","title":"2Sweet-Bomba Latina","duration":213,"url":"http:\/\/cs4341.userapi.com\/u49441496\/audios\/788cd8243842.mp3"},{"aid":186486990,"owner_id":13590837,"artist":"001 Track No05 Latin music 9","title":"001 Track No05 Latin music 9","duration":226,"url":"http:\/\/cs4341.userapi.com\/u25293142\/audios\/277e46d451d4.mp3"},{"aid":185813300,"owner_id":13590837,"artist":"Латино ?? ","title":" Самбо","duration":190,"url":"http:\/\/cs4206.userapi.com\/u2183525\/audios\/678fe97a8700.mp3","lyrics_id":"4944025"},{"aid":185805191,"owner_id":13590837,"artist":"Дженифер Лопес","title":"Латино","duration":212,"url":"http:\/\/cs4220.userapi.com\/u33799853\/audios\/685f4bc7024d.mp3","lyrics_id":"3985793"},{"aid":185355131,"owner_id":13590837,"artist":"Latino","title":"Afa-Na-Na","duration":174,"url":"http:\/\/cs548.userapi.com\/u406078\/audios\/5e771c6958c4.mp3","lyrics_id":"8840070"},{"aid":185167860,"owner_id":13590837,"artist":"Batuka-Latino_StepMIX(137bpm)","title":"demo","duration":232,"url":"http:\/\/cs4863.userapi.com\/u43189860\/audios\/b6a08490146a.mp3","lyrics_id":"10200160"},{"aid":185143167,"owner_id":13590837,"artist":"Pr, Mex","title":"Ставил Windows программист","duration":130,"url":"http:\/\/cs4246.userapi.com\/u3476823\/audios\/75161ed38448.mp3","lyrics_id":"2012814"},{"aid":185141056,"owner_id":13590837,"artist":"Antony Melnyk, Sergiy Tykhanskyy ","title":"Debugging Song","duration":234,"url":"http:\/\/cs6126.userapi.com\/u42350435\/audios\/f83f20d8d754.mp3","lyrics_id":"36053942"},{"aid":185141033,"owner_id":13590837,"artist":"админ","title":"чистый дос","duration":173,"url":"http:\/\/cs4429.userapi.com\/u9853602\/audios\/2b77464f9193.mp3"},{"aid":184547392,"owner_id":13590837,"artist":"Geri Halliwell","title":"Mi chico latino (samba)","duration":194,"url":"http:\/\/cs5057.userapi.com\/u8186180\/audios\/67119f2af914.mp3"},{"aid":184022338,"owner_id":13590837,"artist":"Elena Paparizou","title":"My number one","duration":176,"url":"http:\/\/cs1092.userapi.com\/u830723\/audios\/25552d1f7e40.mp3","lyrics_id":"6640643"},{"aid":183519519,"owner_id":13590837,"artist":"Latino - Samba - Elena Paparizou","title":"Gigolo","duration":203,"url":"http:\/\/cs4405.userapi.com\/u3609345\/audios\/5255ecdda950.mp3","lyrics_id":"7216473"},{"aid":183219402,"owner_id":13590837,"artist":"David Bisbal ","title":" Llorare las penas (самба)","duration":260,"url":"http:\/\/cs5003.userapi.com\/u32245826\/audios\/fe718c40aed1.mp3"},{"aid":183110662,"owner_id":13590837,"artist":"Juanes","title":"La soledad","duration":193,"url":"http:\/\/cs4615.userapi.com\/u400878\/audios\/40abd9dcb4f5.mp3","lyrics_id":"7753114"},{"aid":180455728,"owner_id":13590837,"artist":"Guns N' Roses","title":"Sweet Child O' Mine","duration":356,"url":"http:\/\/cs5125.userapi.com\/u1412326\/audios\/1fc190388445.mp3","lyrics_id":"5582681"},{"aid":180317426,"owner_id":1359083

私が間違っていることと、指定された形式の応答を解析する正しい方法は何ですか? 同じ API を使用するアプリが多数あるため、JSON は正しい..ここで何が問題なのか見当がつかない

4

2 に答える 2

0

不正な引数の例外は、読み取り中に JSON の場合に発生します。これらのURLのために無効なjsonです。また、次の通信のために、logcat をテキスト pls として配置します。

于 2013-02-27T09:39:24.737 に答える
0
Try to display the JSON response in JSONlint.com without this url part of :
"url": "http:\/\/cs510.vkontakte.ru\/u2082836\/audio\/
  d100f76cb84e.mp3"

You will find, the response will be validated properly.

I think there is some space characters present in the url part (between audio\/ and d100f76cb84e.mp3) which is coming from response:
"url": "http:\/\/cs510.vkontakte.ru\/u2082836\/audio\/
  d100f76cb84e.mp3"
于 2013-02-27T10:56:01.347 に答える