以下は私のコードです:
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
},
助けてください。私は今、混乱しています..