gpt4 book ai didi

java - AsyncTask 期间出现 JSONException

转载 作者:行者123 更新时间:2023-12-02 07:14:58 26 4
gpt4 key购买 nike

@Override
protected Integer doInBackground(Void... params)
{
String readMyJSON = readMyJSON();
try {
JSONArray jsonArray = new JSONArray(readMyJSON);
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}

这不起作用,在 JSONArray jsonArray = new JSONArray(readMyJSON); 期间,发生 JSONException。有人能告诉我问题出在哪里吗?提前致谢。

PS:我使用的方法:

   public String readMyJSON() 
{
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://search.twitter.com/search.json?q=android");
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 {
Log.e(ParseJSON.class.toString(), "Failed to download file");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return builder.toString();
}

这个方法看起来还可以。

最佳答案

简而言之,来自 twitter 的响应不是 JSON 数组,因此您会得到一个异常。如果您想要结果数组,请执行以下操作:

JSONObject object = (JSONObject) new JSONTokener(json).nextValue();
JSONArray results = object.getJSONArray("results");

我建议您阅读Twitter API docs 。具体来说,https://dev.twitter.com/docs/api/1/get/search .

关于java - AsyncTask 期间出现 JSONException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15046313/

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