【问题标题】:Android - StackOverflow APIAndroid - StackOverflow API
【发布时间】:2012-05-02 05:33:47
【问题描述】:

我正在尝试从 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/"

【问题讨论】:

标签: android httprequest stackexchange-api


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2016-07-19
    • 1970-01-01
    • 2011-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多