Java - Reader无法解析

时间:2013-11-25 10:32:22

标签: java json twitter oauth-2.0 try-catch-finally

我的java语法有点麻烦,我很难理解错误。问题出在我的BufferedReader中:“finally {reader.close();}”它说读者无法解决,但在它之前的“尝试”中,它没有问题就解决了。这是有问题的代码部分。我只是尝试使用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");
        }       
    }
 }

2 个答案:

答案 0 :(得分:1)

reader之外声明try

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

答案 1 :(得分:1)

您的BufferedReader范围不正确。在try块之外声明它。

  BufferedReader reader =null;// Declare here.

  try{
     ............
   reader = new BufferedReader(
                new InputStreamReader(inputStream, "UTF-8"), 8);

   }
   finally{
    reader.close();
   }