Get a list of all forwarded users of a Sina Weibo (using the Repost_timeline interface)

Source: Internet
Author: User
Tags json

When using the Sina Weibo API for projects, you need to get a list of all the users who forwarded a particular microblog. decided to use Statuses/repost_timeline to get a forwarding list, but found two questions:

1: Sina Weibo has restrictions on this interface and can only return the latest 2000 data. The official document says: This interface returns up to the latest 2000 data only. Practice has found that once the number of fetches is about to exceed 2000, the outgoing request returns the error message shown in "Code 1".

2: Even if I want to compromise, with only 2000 data, Using Sina Weibo to provide the official JAVASDK of the corresponding test class Getreposttimeline.java, each request can only return 20 data, want to multiple requests to gather enough of all the user list but found that the results of multiple requests returned actually have a duplicate (and test permissions per hour only a valuable 150 requests, do not want this Waste).


After several attempts, it is found that the second problem can be solved by the two parameters of the Repost_timeline interface:

Count Single page returns the number of records, maximum no more than 200, default is 20.

Page Returns the page number of the result, which defaults to 1.

That is, as long as the number of records returned by the modified single page is 200, and the loop 10 times to the server to send a request, each request to change the page number of the returned results pages, you can access to the number of users to forward a microblog (2000) of the list. Code is proven feasible, the code is shown in "Code 2":

As a results, not only to get nearly 2000 data, the number of requests are also controlled to a minimum. The same changes apply to other APIs with the count and page parameters of Sina Weibo. But the need for a "list of all forwarded users who want to get a micro-blog up to Now" is not met, after all, they only provide the latest 2000 data, multiple requests to fill all the user list. Ask you crossing answer ....

//code 1 weibo4j.model.weiboexception:a jsonobject text must begin with ' {' at character 1:[] at weibo4j . http. Response.asjsonobject (response.java:201) at Weibo4j.model.Status.constructWapperStatus (status.java:263) at weibo4j . Timeline.getreposttimeline (timeline.java:308) at VisProject.GetRepostList.main (getrepostlist.java:33) caused by: Weibo4j.org.json.jsonexception:a Jsonobject text must begin with ' {' at character 1 at Weibo4j.org.json.JSONTokener.synt Axerror (jsontokener.java:410) at weibo4j.org.json.jsonobject.<init> (jsonobject.java:179) at Weibo4j.org.json.jsonobject.<init> (jsonobject.java:402) at Weibo4j.http.Response.asJSONObject ( response.java:199) ... 3 more 
<pre code_snippet_id= "221346" snippet_file_name= "blog_20140306_1_2099870" name= "code" class= "Java" >//code 2

Import java.util.List; Import weibo4j.
Timeline;
Import weibo4j.model.Paging;
Import Weibo4j.model.Status;
Import Weibo4j.model.StatusWapper;

Import weibo4j.model.WeiboException;   public class Getrepostlist {public static void main (string[] args) {//authorization String Access_token = "";
		Accesstoken Timeline TM = new Timeline () before the application is placed here;
		
		Tm.client.setToken (Access_token);   String id = "";
		Here put the ID of the forwarded Weibo//Set Forward list page Paging page = new Paging (); Page.setcount (200);   Set to the maximum value of the capacity of each page int i=0; Test with int flag = 200;
			The control loop jumps out, reducing the number of API requests for (int pageindex=1; pageindex<10; pageindex++) {page.setpage (pageIndex);
				try {if (flag <) break;
				Statuswapper status = Tm.getreposttimeline (ID, page); 
                                list<status> resultlist = status.getstatuses ();
			  Flag = Resultlist.size ();      if (resultlist! = null && resultlist.size () > 0) {//If the obtained content is empty, exit for (Status S:resu  Ltlist) {System.out.println (S.getuser (). GetName ());
				    The operation of each forwarding item is placed in the for each function, where the user name of the forward user i++ is displayed;
			}}else{break;}
			} catch (Weiboexception e) {e.printstacktrace ();
	}} System.out.println ("The number of Retwitter catched this time is:" +i); }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.