Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JSONException Twitter客户端应用程序_Java_Json_Twitter_Twitter4j - Fatal编程技术网

Java JSONException Twitter客户端应用程序

Java JSONException Twitter客户端应用程序,java,json,twitter,twitter4j,Java,Json,Twitter,Twitter4j,我目前正在尝试为twitter构建一个客户端应用程序。该应用程序的功能之一是搜索tweet(包括历史tweet)。我试图修改从Github获得的代码。然而,当我试图调试代码时,我得到了由null值引起的JSONException。这是我的密码: package thematicanalysis; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import j

我目前正在尝试为twitter构建一个客户端应用程序。该应用程序的功能之一是搜索tweet(包括历史tweet)。我试图修改从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();
        }
    }  
}

你不需要刮Twitter,使用类似Twitter4J的东西。看看他们的推文吧。@jonathan实际上,我的目标之一是从twitter上找到旧的推文,这恐怕是用Twitter4J无法做到的