gpt4 book ai didi

android - 在 org.json.JSON.typeMismatch(JSON.java :107)

转载 作者:行者123 更新时间:2023-11-29 16:08:29 24 4
gpt4 key购买 nike

下面是我的代码:

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();
}
}

我收到错误消息:

Invalid cookie header: "set-cookie: guest_id=v1%3A136496691433468960; Domain=.twitter.com; Path=/; Expires=Fri, 03-Apr-2015 05:28:34 UTC". Unable to parse expires attribute: Fri, 03-Apr-2015 05:28:34 UTC org.json.JSONException: Value [{"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":"http://twitter.com/search?q=%23PSG","promoted_content":null,"name":"#PSG"},{"events":null,"query":"%23IPLOpeningCeremony","url":"http://twitter.com/search?q=%23IPLOpeningCeremony","promoted_content":null,"name":"#IPLOpeningCeremony"},{"events":null,"query":"%23UCL","url":"http://twitter.com/search?q=%23UCL","promoted_content":null,"name":"#UCL"},{"events":null,"query":"%23GamesWePlay","url":"http://twitter.com/search?q=%23GamesWePlay","promoted_content":null,"name":"#GamesWePlay"},{"events":null,"query":"Juve","url":"http://twitter.com/search?q=Juve","promoted_content":null,"name":"Juve"},{"events":null,"query":"Valdes","url":"http://twitter.com/search?q=Valdes","promoted_content":null,"name":"Valdes"},{"events":null,"query":"Barca","url":"http://twitter.com/search?q=Barca","promoted_content":null,"name":"Barca"},{"events":null,"query":"Bayern","url":"http://twitter.com/search?q=Bayern","promoted_content":null,"name":"Bayern"}],"locations":[{"woeid":23424848,"name":"India"}],"created_at":"2013-04-03T05:19:03Z"}] of type org.json.JSONArray cannot be converted to JSONObject at org.json.JSON.typeMismatch(JSON.java:107)

谁能指导我哪里做错了?

这个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
},

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

最佳答案

在 logcat 结果中:

JSONArray cannot be converted to 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


},
.....
]

关于android - 在 org.json.JSON.typeMismatch(JSON.java :107),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15779560/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com