HttpURLConnection - 完成后的JSON响应

时间:2012-05-27 12:40:42

标签: java android json post payload

我正在尝试使用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)项目上运行代码,我得到以下output。虽然,如果我在Android上运行它,日志会显示以下内容:

{ “报头”:{ “主机名”: “RHL073”}, “结果”:{ “歌曲”:[{ “SongID”:5443351, “SONGNAME”:“?????? \ U00B7 ?? ??(FINAL FANTASY XII ????)“,”ArtistID“:713,”ArtistName“:”最终幻想“,”AlbumID“:898007,”AlbumName“:”最终幻想XII原声带“,”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库。我真的不明白这里的问题是什么。有没有人知道为什么?

0 个答案:

没有答案