JSONException Twitter客户端应用程序

时间:2015-04-26 22:08:59

标签: java json twitter twitter4j

我目前正在尝试为twitter构建客户端应用程序。该应用程序的功能之一是搜索推文(包括历史推文)。我试图修改我从Github获得的代码。但是,当我尝试调试代码时,我得到了由null值引起的JSONException。这是我的代码:

package thematicanalysis;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Date;
import twitter4j.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import twitter4j.JSONException;

/**
 *
 * @author adichris
 */
public class TweetManager {


    private static String getURLResponse(String since, String until, String querySearch, String scrollCursor,int counter) throws Exception{
        String appendQuery = " ";
        if(since!=null)
            appendQuery+= " since:"+since;
        if(until!=null)
            appendQuery+= " until:"+until;
        if(querySearch!=null)
            appendQuery+= " "+querySearch;
        String url = String.format("https://twitter.com/search?src=typd&q=%s&scroll_cursor=%s", URLEncoder.encode(appendQuery, "UTF-8"),scrollCursor);
            System.out.println("URL: "+ url);
        URL obj = new URL (url);
        HttpURLConnection con = (HttpURLConnection)obj.openConnection();
        con.setRequestMethod("GET");
        //StringBuilder response;
         BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
         String inputLine;
         StringBuffer response = new StringBuffer();
         while((inputLine=in.readLine())!=null)
                response.append(inputLine);
         in.close();
         con.disconnect();
            //System.out.println(response.toString());
        saveToFile(response.toString(),counter);
     return response.toString();
    }

    private static void saveToFile(String content,int counter) throws IOException
    {
        try (PrintWriter pw = new PrintWriter("newOutput"+counter+".txt","UTF-8")) {
            pw.printf("%s\n",content);
            pw.close();
        }
    }

    public static void getTweets (String since, String until, String querySearch) throws JSONException, Exception{
     try{
        String refreshCursor = null;
        int counter = 1;
       while(true)
       {

           String response = getURLResponse(since,until,querySearch,refreshCursor,counter);
           JSONObject json = new JSONObject(response);
           if(json.equals(null))
               System.out.println("hereeee");
           counter++;
            System.out.println(counter);
            refreshCursor = json.getString("scroll_cursor");
            Document doc = Jsoup.parse((String)json.get("items_html"));
            Elements tweets = doc.select("div.js-stream-tweet");
            System.out.println(tweets.size());
            if (tweets.isEmpty()){
                break;
            }
            for (Element tweet: tweets){
            String userName = tweet.select("span.username.js-action-profile-name b").text();
            String text = tweet.select("p.js-tweet-text").text().replaceAll("[^\\u0000-\\uFFFF]", "");
            long dateMs = Long.valueOf(tweet.select("small.time span.js-short-timestamp").attr("data-time-ms"));
            Date date = new Date(dateMs);
            System.out.println(userName);
            //saveToFile(text);
            }
        }
        }catch(JSONException e){
        e.printStackTrace();
        }
    }  
}

0 个答案:

没有答案
相关问题