1

我正在尝试使用 POST Payload 及其请求的方法向Grooveshark API发送请求,但我发现了一个问题。请允许我先向您展示我的代码。

public void getResponse() throws Exception
{
    if(service.equals("Grooveshark")) link += getHmacMD5(privateGroovesharkKey, jsonInfo.toString());

    if(requestedMethod.equals("GET")) infoURL = new URL(link+arguments);
    else infoURL = new URL(link);

    HttpURLConnection connection = (HttpURLConnection) infoURL.openConnection();
    connection.setRequestMethod(requestedMethod);
    connection.setRequestProperty("Accept-Charset", "UTF-8");

connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);

if(service.equals("Grooveshark"))
{
    connection.setRequestProperty("Content-Type","application/json");
        OutputStream output = connection.getOutputStream();
        output.write(jsonInfo.toString().getBytes());
}
else if(requestedMethod.equals("POST") || requestedMethod.equals("PUT"))
{
    OutputStream output = connection.getOutputStream();
        output.write(arguments.getBytes());
}

    connection.connect();

    BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    StringBuilder sb = new StringBuilder();

    String line;
    while ((line = rd.readLine()) != null) 
        sb.append(line).append('\n');

    setJsonResult(sb.toString());

    System.out.println(jsonResult);

    jsonFinal = new JSONObject(jsonResult);

    connection.disconnect();
}

我已经在我的项目中获得了该代码,并且我可以成功地将请求发送到在其响应中使用 JSON 的任何 API Web 服务。现在只有一个问题:在 Android 中,它并没有给我完整的答案。我尝试在单独的 Java(非 Android)项目上运行代码,得到以下输出。虽然,如果我在 Android 上运行它,日志会显示以下内容:

{"header":{"hostname":"RHL073"},"result":{"songs":[{"SongID":5443351,"SongName":"??????\u00b7??( FINAL FANTASY XII????)","ArtistID":713,"ArtistName":"Final Fantasy","AlbumID":898007,"AlbumName":"Final Fantasy XII Original Soundtrack","CoverArtFilename":"", “人气”:1214500005,“IsLowBitrateAvailable”:tr

它停在那个tr上。这与我之后实际应用的文件的解析有关吗?我不认为是这样,但以防万一,这里是[这就是我调用搜索的方式,JSONHandler 是包含上面提供的代码的对象]:

public void performSearch() throws Exception
{
    JSONObject search = new JSONObject();
    search.put("method", method);

    JSONObject header = new JSONObject();
    header.put("wsKey", key);

    JSONObject parameters = new JSONObject();
    parameters.put("query", getSearchQuery());
    parameters.put("country", "Portugal");
    parameters.put("limit", limit);
    parameters.put("offset", "");

    search.put("header", header);
    search.put("parameters", parameters);

    JSONHandler jsonHandler = new JSONHandler(link, search, "Grooveshark", "POST", "");

    JSONObject finalResult = jsonHandler.getJsonFinal();

    JSONArray songs = finalResult.getJSONObject("result").getJSONArray("songs");

    ArrayList<Result> allResults = new ArrayList<Result>();

    for(int i = 0; i < songs.length(); i++)
    {
        JSONObject inner = (JSONObject) songs.get(i);
        String name = inner.getString("SongName");
        int ID = inner.getInt("SongID");
        String artist = inner.getString("ArtistName");
        Result res = new Result(name, artist, ID);
        res.setAlbumName(inner.getString("AlbumName"));

        boolean low = inner.getBoolean("IsLowBitrateAvailable");
        int bit = 0;

        if(low) bit = 1;
        else bit = 0;

        res.setIsLowBitRateAvailable(bit);
    }

    setResults(allResults);
}

如您所见,我使用的是 json.org 库。我真的不明白这里有什么问题。有没有人知道为什么?

4

0 回答 0