1

下面是我的代码:

public class JSON extends Activity {
    TextView json;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String twitterTimeline = getTwitterTimeline();
        try {
            String tweets = "";
            JSONObject jObj = new JSONObject(twitterTimeline);
            JSONArray jsonArray = jObj.getJSONArray("results");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                int j = i+1;
                tweets +="*** " + j + " ***\n";
                tweets += "Date:" + jsonObject.getString("trends") + "\n";
                tweets += "Post:" + jsonObject.getString("name") + "\n\n";
            }
            json= (TextView)findViewById(R.id.json);
            json.setText(tweets);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    public String getTwitterTimeline() {
        StringBuilder builder = new StringBuilder();
        HttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet("https://api.twitter.com/1/trends/23424848.json");
        try {
            HttpResponse response = client.execute(httpGet);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (statusCode == 200) {
                HttpEntity entity = response.getEntity();
                InputStream content = entity.getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(content));
                String line;
                while ((line = reader.readLine()) != null) {
                    builder.append(line);
                }
            } else {
                //Couldn't obtain the data
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return builder.toString();
    }
}

我收到错误消息:

cookie 标头无效:“set-cookie:guest_id=v1%3A136496691433468960;Domain=.twitter.com;Path=/;Expires=Fri,2015 年 4 月 3 日 05:28:34 UTC”。无法解析过期属性:Fri, 03-Apr-2015 05:28:34 UTC org.json.JSONException: Value [{"as_of":"2013-04-03T05:28:34Z","trends":[{ "事件":null,"query":"%22Finding+Dory%22","url":"http://twitter.com/search?q=%22Finding+Dory%22","promoted_content":null, "name":"Finding Dory"},{"events":null,"query":"%23MentionADislike","url":"http://twitter.com/search?q=%23MentionADislike","promoted_content" :null,"name":"#MentionADislike"},{"events":null,"query":"%23PSG","url"

谁能指导我哪里做错了?

这个 JSON 格式如何解析和显示

[
{
"trends":[
{
"name":"#PappuCII",
"url":"http:\/\/twitter.com\/search?q=%23PappuCII",
"promoted_content":null,
"query":"%23PappuCII",
"events":null
},
{
"name":"#ReplaceMovieNamesWithKamina",
"url":"http:\/\/twitter.com\/search?q=%23ReplaceMovieNamesWithKamina",
"promoted_content":null,
"query":"%23ReplaceMovieNamesWithKamina",
"events":null
},

请帮帮我,我现在很困惑..

4

1 回答 1

4

如 logcat 结果:

JSONArray 无法转换为 JSONObject

因为您JSONArray在当前 json 字符串中作为根元素而不是JSONObject. 因此,您需要先将当前字符串转换为 JSONArray,然后从中提取所有 JSONObject。将您的代码更改为:

JSONArray jArray = new JSONArray (twitterTimeline); //<convert string to JSONArray 
for (int i = 0; i < jArray .length(); i++) {
  // get all JSONObject from jArray  here..
 }

您当前的 json 格式是:

[    //<<<< this is JSONArray 
  {  //<<<< this is JSONObject  inside JSONArray 


  },
 .....
]
于 2013-04-03T05:33:28.877 回答