Android - StackOverflow API

时间:2012-04-20 15:55:42

标签: android httprequest stackexchange-api

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

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

我得到这样的东西:

enter image description here

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/"

1 个答案:

答案 0 :(得分:2)

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

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

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