Search in sources :

Example 1 with PostParameter

use of weibo4j.model.PostParameter in project twitter-2-weibo by rjyo.

the class HttpClient method get.

public Response get(String url, PostParameter[] params, Paging paging) throws WeiboException {
    if (null != paging) {
        List<PostParameter> pagingParams = new ArrayList<PostParameter>(4);
        if (-1 != paging.getMaxId()) {
            pagingParams.add(new PostParameter("max_id", String.valueOf(paging.getMaxId())));
        }
        if (-1 != paging.getSinceId()) {
            pagingParams.add(new PostParameter("since_id", String.valueOf(paging.getSinceId())));
        }
        if (-1 != paging.getPage()) {
            pagingParams.add(new PostParameter("page", String.valueOf(paging.getPage())));
        }
        if (-1 != paging.getCount()) {
            if (-1 != url.indexOf("search")) {
                // search api takes "rpp"
                pagingParams.add(new PostParameter("rpp", String.valueOf(paging.getCount())));
            } else {
                pagingParams.add(new PostParameter("count", String.valueOf(paging.getCount())));
            }
        }
        PostParameter[] newparams = null;
        PostParameter[] arrayPagingParams = pagingParams.toArray(new PostParameter[pagingParams.size()]);
        if (null != params) {
            newparams = new PostParameter[params.length + pagingParams.size()];
            System.arraycopy(params, 0, newparams, 0, params.length);
            System.arraycopy(arrayPagingParams, 0, newparams, params.length, pagingParams.size());
        } else {
            if (0 != arrayPagingParams.length) {
                String encodedParams = HttpClient.encodeParameters(arrayPagingParams);
                if (-1 != url.indexOf("?")) {
                    url += "&" + encodedParams;
                } else {
                    url += "?" + encodedParams;
                }
            }
        }
        return get(url, newparams);
    } else {
        return get(url, params);
    }
}
Also used : PostParameter(weibo4j.model.PostParameter) ArrayList(java.util.ArrayList)

Example 2 with PostParameter

use of weibo4j.model.PostParameter in project twitter-2-weibo by rjyo.

the class HttpClient method multPartURL.

/**
	 * 支持multipart方式上传图片
	 * 
	 */
public Response multPartURL(String url, PostParameter[] params, ImageItem item) throws WeiboException {
    PostMethod postMethod = new PostMethod(url);
    try {
        Part[] parts = null;
        if (params == null) {
            parts = new Part[1];
        } else {
            parts = new Part[params.length + 1];
        }
        if (params != null) {
            int i = 0;
            for (PostParameter entry : params) {
                parts[i++] = new StringPart(entry.getName(), (String) entry.getValue());
            }
            parts[parts.length - 1] = new ByteArrayPart(item.getContent(), item.getName(), item.getContentType());
        }
        postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
        return httpRequest(postMethod);
    } catch (Exception ex) {
        throw new WeiboException(ex.getMessage(), ex, -1);
    }
}
Also used : WeiboException(weibo4j.model.WeiboException) PostParameter(weibo4j.model.PostParameter) PostMethod(org.apache.commons.httpclient.methods.PostMethod) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) Part(org.apache.commons.httpclient.methods.multipart.Part) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) JSONException(weibo4j.org.json.JSONException) IOException(java.io.IOException) WeiboException(weibo4j.model.WeiboException)

Example 3 with PostParameter

use of weibo4j.model.PostParameter in project twitter-2-weibo by rjyo.

the class HttpClient method multPartURL.

public Response multPartURL(String fileParamName, String url, PostParameter[] params, File file, boolean authenticated) throws WeiboException {
    PostMethod postMethod = new PostMethod(url);
    try {
        Part[] parts = null;
        if (params == null) {
            parts = new Part[1];
        } else {
            parts = new Part[params.length + 1];
        }
        if (params != null) {
            int i = 0;
            for (PostParameter entry : params) {
                parts[i++] = new StringPart(entry.getName(), (String) entry.getValue());
            }
        }
        FilePart filePart = new FilePart(fileParamName, file.getName(), file, new MimetypesFileTypeMap().getContentType(file), "UTF-8");
        filePart.setTransferEncoding("binary");
        parts[parts.length - 1] = filePart;
        postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
        return httpRequest(postMethod);
    } catch (Exception ex) {
        throw new WeiboException(ex.getMessage(), ex, -1);
    }
}
Also used : MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) WeiboException(weibo4j.model.WeiboException) PostParameter(weibo4j.model.PostParameter) PostMethod(org.apache.commons.httpclient.methods.PostMethod) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) Part(org.apache.commons.httpclient.methods.multipart.Part) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) JSONException(weibo4j.org.json.JSONException) IOException(java.io.IOException) WeiboException(weibo4j.model.WeiboException)

Aggregations

PostParameter (weibo4j.model.PostParameter)3 IOException (java.io.IOException)2 PostMethod (org.apache.commons.httpclient.methods.PostMethod)2 FilePart (org.apache.commons.httpclient.methods.multipart.FilePart)2 MultipartRequestEntity (org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity)2 Part (org.apache.commons.httpclient.methods.multipart.Part)2 StringPart (org.apache.commons.httpclient.methods.multipart.StringPart)2 WeiboException (weibo4j.model.WeiboException)2 JSONException (weibo4j.org.json.JSONException)2 ArrayList (java.util.ArrayList)1 MimetypesFileTypeMap (javax.activation.MimetypesFileTypeMap)1