Example usage for twitter4j RequestMethod POST

List of usage examples for twitter4j RequestMethod POST

Introduction

In this page you can find the example usage for twitter4j RequestMethod POST.

Prototype

RequestMethod POST

To view the source code for twitter4j RequestMethod POST.

Click Source Link

Usage

From source file:cmu.edu.homework.mediaUpload.AbstractPhotoUploadImpl.java

License:Apache License

private String post() {
    uploadUrl = "https://upload.twitter.com/1.1/media/upload.json";
    HttpParameter[] params = new HttpParameter[1];
    try {//from w  w  w.j  ava 2s . co  m
        InputStream is = new FileInputStream(new File(this.photo));
        params[0] = new HttpParameter("media", photo, is);
        postParameter = params;
    } catch (Exception e) {
        e.printStackTrace();
    }
    headers.putAll(client.getRequestHeaders());
    HttpRequest req = new HttpRequest(RequestMethod.POST, uploadUrl, postParameter, null, headers);
    String authheader = oauth.getAuthorizationHeader(req);
    headers.put("Authorization", authheader);
    HttpRequest req2 = new HttpRequest(RequestMethod.POST, uploadUrl, postParameter, null, headers);
    try {
        httpResponse = client.request(req2, null);
        if (httpResponse.getStatusCode() != 202) {
            Log.e("Error from Twitter", "get error in post:" + httpResponse.getStatusCode() + ", details:"
                    + httpResponse.toString());
        }
        return httpResponse.asJSONObject().getString("media_id_string");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:cmu.edu.homework.mediaUpload.AbstractVideoUploadImpl.java

License:Apache License

private String post(String message, long[] media_ids) {
    String postUrl = "https://api.twitter.com/1.1/statuses/update.json";
    HttpParameter[] params = new HttpParameter[2];
    params[0] = new HttpParameter("status", message);
    params[1] = new HttpParameter("media_ids", String.valueOf(media_ids));
    postParameter = params;/*w w  w  .j a v a  2  s.  c  om*/
    headers.putAll(client.getRequestHeaders());
    String authheader = generateVerifyCredentialsAuthorizationHeader("POST");
    headers.put("Authorization", authheader);

    HttpRequest req2 = new HttpRequest(RequestMethod.POST, uploadUrl, postParameter, null, headers);
    try {
        httpResponse = client.request(req2, null);
        if (httpResponse.getStatusCode() != 202) {
            Log.e("Error from Twitter", "get error in INIT:" + httpResponse.getStatusCode() + ", details:"
                    + httpResponse.toString());
        }
        return "202";
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "Error";
}

From source file:cmu.edu.homework.mediaUpload.AbstractVideoUploadImpl.java

License:Apache License

private String init() {
    uploadUrl = "https://upload.twitter.com/1.1/media/upload.json";
    HttpParameter[] params = new HttpParameter[3];
    params[0] = new HttpParameter("command", "INIT");
    long total_bytes = new File(video).length();
    params[1] = new HttpParameter("total_bytes", total_bytes);
    params[2] = new HttpParameter("media_type", "video/mp4");

    if (this.postParameter != null && this.postParameter.length > 0) {
        this.appendHttpParameters(params, this.postParameter);
    } else {/* w  w  w .  j a v  a 2s . co  m*/
        this.postParameter = params;
    }
    headers.putAll(client.getRequestHeaders());

    HttpRequest req = new HttpRequest(RequestMethod.POST, uploadUrl, postParameter, null, headers);
    String authheader = oauth.getAuthorizationHeader(req);
    headers.put("Authorization", authheader);
    HttpRequest req2 = new HttpRequest(RequestMethod.POST, uploadUrl, postParameter, null, headers);
    try {
        httpResponse = client.request(req2, null);

        if (httpResponse.getStatusCode() != 202) {
            Log.e("Error from Twitter", "get error in INIT:" + httpResponse.getStatusCode() + ", details:"
                    + httpResponse.toString());
        }
        return httpResponse.asJSONObject().getString("media_id_string");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:cmu.edu.homework.mediaUpload.AbstractVideoUploadImpl.java

License:Apache License

private String append(String media_id_string) {

    HttpParameter[] params = new HttpParameter[4];
    params[0] = new HttpParameter("command", "APPEND");
    params[1] = new HttpParameter("media_id", media_id_string);
    params[2] = new HttpParameter("segment_index", "0");
    try {//from   w ww  . ja  va2  s  .c o  m
        InputStream is = new FileInputStream(new File(video));
        params[3] = new HttpParameter("media", video, is);
    } catch (Exception e) {
        e.printStackTrace();
    }

    if (this.postParameter != null && this.postParameter.length > 0) {
        this.appendHttpParameters(params, this.postParameter);
    } else {
        this.postParameter = params;
    }
    headers.putAll(client.getRequestHeaders());

    HttpRequest req = new HttpRequest(RequestMethod.POST, uploadUrl, postParameter, null, headers);

    String authheader = oauth.getAuthorizationHeader(req);
    headers.put("Authorization", authheader);
    HttpRequest req2 = new HttpRequest(RequestMethod.POST, uploadUrl, postParameter, null, headers);
    try {
        httpResponse = client.request(req2, null);
        if (httpResponse.getStatusCode() != 202) {
            Log.e("Error from Twitter", "get error in INIT:" + httpResponse.getStatusCode() + ", details:"
                    + httpResponse.toString());
        }
        return httpResponse.asJSONObject().getString("media_id_string");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:cmu.edu.homework.mediaUpload.AbstractVideoUploadImpl.java

License:Apache License

private String finalize(String media_id_string) {
    HttpParameter[] params = new HttpParameter[2];
    params[0] = new HttpParameter("command", "FINALIZE");
    params[1] = new HttpParameter("media_id", media_id_string);

    if (this.postParameter != null && this.postParameter.length > 0) {
        this.appendHttpParameters(params, this.postParameter);
    } else {/*from w  ww . ja v  a2  s  . c  o m*/
        this.postParameter = params;
    }
    headers.putAll(client.getRequestHeaders());

    HttpRequest req = new HttpRequest(RequestMethod.POST, uploadUrl, postParameter, null, headers);

    String authheader = oauth.getAuthorizationHeader(req);
    headers.put("Authorization", authheader);
    HttpRequest req2 = new HttpRequest(RequestMethod.POST, uploadUrl, postParameter, null, headers);
    try {
        httpResponse = client.request(req2, null);
        if (httpResponse.getStatusCode() != 202) {
            Log.e("Error from Twitter", "get error in INIT:" + httpResponse.getStatusCode() + ", details:"
                    + httpResponse.toString());
        }
        Log.d(TAG, "Response after finalized:" + httpResponse.asString());
        return httpResponse.asJSONObject().getString("media_id_string");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;

}