gpt4 book ai didi

Java - Reader 无法解析

转载 作者:行者123 更新时间:2023-12-02 06:29:34 26 4
gpt4 key购买 nike

我的 java 语法有点问题,我很困惑到底出了什么问题。问题出在我的 BufferedReader 中:“finally{reader.close();}”,它说 reader 无法解析,但在它之前的“try”中,它已经解决,没有问题。这是有问题的代码部分。我只是尝试使用 oAuth 2.0 授权将拉取的 Twitter API 1.1 JSON 结果解析为易于阅读的内容。我将不胜感激。

    /////////////////////////////////////////////////////////////
// AsyncTask - Download Twitter Feed w/Token as authorization
// //////////////////////////////////////////////////////////

protected class loadTwitterFeed extends AsyncTask<Void, Void, Integer> {

@Override
protected Integer doInBackground(Void... params) {

try{
DefaultHttpClient httpclient = new DefaultHttpClient(
new BasicHttpParams());
HttpGet httpget = new HttpGet(tweeterURL);
httpget.setHeader("Authorization", "Bearer " + twitterToken);
httpget.setHeader("Content-type", "application/json");

InputStream inputStream = null;
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();

inputStream = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(inputStream, "UTF-8"), 8);
return null;
}

finally{
reader.close();
}
}

protected void onPostExecute(String result) {

StringBuilder sb = new StringBuilder();
try{
JSONObject resultObject = new JSONObject(result);
org.json.JSONArray tweetArray = resultObject.getJSONArray("results");
for (int t=0; t<tweetArray.length(); t++) {
JSONObject tweetObject = tweetArray.getJSONObject(t);
sb.append(tweetObject.getString("from_user")+": ");
sb.append(tweetObject.get("text")+"\n\n");
}
}
catch (Exception e) {
Log.e("Tweet", "Error retrieving JSON stream" + e.getMessage());
jsonFeed = sb.toString();
e.printStackTrace();
}

}
}
/* String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
} */



protected void onPostExecute(Integer result) {
// Update GUI
if (jsonFeed.length()>0){
twitterText.setText(jsonFeed.toString());
//if (jsonFeed.length() > 0) {
//twitterText.setText(jsonFeed);
} else {
//I'd assume wrong Consumer Key/Secret if this happens.
twitterText.setText("Nothing Returned");
}
}
}

最佳答案

try 之外声明 reader:

BufferedReader reader = null;
try {
reader = ..
..
}
finally {
if(reader != null)
reader.close();
}

关于Java - Reader 无法解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20189853/

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