skip to Main Content

I want to get user tweets with http request to twitter api.

@Override
public void run() {
    try {
        URL url = new URL("https://api.twitter.com/1/statuses/home_timeline.json");
        HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
        httpCon.setDoOutput(true);
        httpCon.setRequestMethod("GET"); //same error with POST
        OutputStreamWriter out = new OutputStreamWriter(httpCon.getOutputStream());
        System.out.println(httpCon.getResponseCode());
        System.out.println(httpCon.getResponseMessage());
        out.close();
    } catch (Exception e) {
        System.out.println(e);
        e.printStackTrace();
    }
}

But I am getting error 404. If I change version from 1.1 to 1 the it gives me error 410.
I have already created my account in developer site and have generated all of my tokens and keys.
can Anybody tell me how to authenticate to twitter?

NOTE:I am able to sign up with fabric sdk.

Regards.

3

Answers


  1. Rather using plane old URL class i would recomend to use any standard API to communicate with twitter like http://twitter4j.org/

    Code sample to check timeline

        Twitter twitter = TwitterFactory.getSingleton();
        List<Status> statuses = twitter.getHomeTimeline();
        System.out.println("Showing home timeline.");
        for (Status status : statuses) {
            System.out.println(status.getUser().getName() + ":" + status.getText());
        }
    

    To check other users time line :

    Twitter unauthenticatedTwitter = new TwitterFactory().getInstance();
    //First param of Paging() is the page number, second is the number per page (this is capped around 200 I think.
    Paging paging = new Paging(1, 100);
    List<Status> statuses = unauthenticatedTwitter.getUserTimeline("ameyjadiye",paging);
    
    Login or Signup to reply.
  2. This is full code Twitter.

    https://github.com/shivagss/AndroidTwitterClient

    Hope. It will help you !!!

    Login or Signup to reply.
  3. There are different libraries to get Twitter Tweets like:

    1.Twitter4j Library

    1. Fabric by Twitter: fabric.io

    If you are using Android studio then it has Android Studio Plugin which will configure easily Twitter.

    Hope this help.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search