1

我正在尝试从 stackexchange api 中检索一些数据。我做的很简单:

https://api.stackexchange.com/2.0/questions?order=desc&sort=activity&tagged=android&site=stackoverflow

我得到这样的东西:

在此处输入图像描述

public JSONArray latestQuestions(String tagged)
{
    String result = "";
    JSONObject jArray = null;
    JSONArray questions = null;

    String link = api + "questions?order=desc&sort=activity&site=stackoverflow";

    if(tagged != null)
    {           
        link = api + "questions?order=desc&sort=activity&tagged=" + tagged + "&site=stackoverflow";
    }

    DefaultHttpClient client = new DefaultHttpClient();
    try
    {
        Log.i(TAG + " latestQuestions",link);
        HttpGet getQ = new HttpGet(link);
        HttpResponse qResponse = client.execute(getQ);
        HttpEntity entity = qResponse.getEntity();                     
        if (entity != null) 
        {
            result= EntityUtils.toString(entity, HTTP.UTF_8);   
            Log.d("JSON",result);
        }
    }
    catch(Exception e)
    {   
        Log.e(TAG + " latestQuestions","LOADING ERROR");
    }

    try
    {
        jArray = new JSONObject(result);
    }
    catch(JSONException e)
    {
        Log.e(TAG + " latestQuestions","JSON parsing error");
    }

    if(jArray != null)
    {
        try
        {
            questions = jArray.getJSONArray("items");
        }
        catch(JSONException e)
        {
            Log.d("JSON",result.toString());
        }
    }

    return questions;
}

在哪里:

String api = "https://api.stackexchange.com/2.0/"
4

1 回答 1

3

响应应该被压缩...... GZip......查看他们的一些文档。

特别是:https ://api.stackexchange.com/docs/compression

要处理解压缩数据,请查看此链接:http: //developer.android.com/reference/java/util/zip/GZIPInputStream.html

于 2012-04-27T14:48:41.947 回答