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, 03-Apr-2015 05:28:34 UTC". 期限切れ属性を解析できません: Fri, 03-Apr-2015 05:28:34 UTC org.json.JSONException: 値 [{"as_of":"2013-04-03T05:28:34Z","trends":[{ "events":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 に答える