-2

借助最简单的 Java 示例使用 twitter API 版本 1.1 检索 user_timeline,我正在尝试检索用户 twiter 消息。但面临三个问题。我需要使用android 2.3 如果可以请帮忙

    public void testUserTimelineWithParams() throws Exception {
    //This will read the timeline of the 'twitterapi' account.

    String method = "GET";
    String url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
    List<NameValuePair> urlParams = new ArrayList<NameValuePair>();
    urlParams.add( new NameValuePair("screen_name","twitterapi") );
    urlParams.add( new NameValuePair("count", "10") );

    String oAuthConsumerKey = "Your value";
    String oAuthConsumerSecret = "Your value"; //<--- DO NOT SHARE THIS VALUE

    String oAuthAccessToken = "Your value";
    String oAuthAccessTokenSecret = "Your value"; //<--DO NOT SHARE THIS VALUE

    String oAuthNonce = String.valueOf(System.currentTimeMillis());
    String oAuthSignatureMethod = "HMAC-SHA1";
    String oAuthTimestamp = time();
    String oAuthVersion = "1.0";

    String signatureBaseString1 = method;
    String signatureBaseString2 = url;

    List<NameValuePair> allParams = new ArrayList<NameValuePair>();

    allParams.add(new NameValuePair("oauth_consumer_key", oAuthConsumerKey));
    allParams.add(new NameValuePair("oauth_nonce", oAuthNonce));
    allParams.add(new NameValuePair("oauth_signature_method", oAuthSignatureMethod));
    allParams.add(new NameValuePair("oauth_timestamp", oAuthTimestamp));
    allParams.add(new NameValuePair("oauth_token", oAuthAccessToken));
    allParams.add(new NameValuePair("oauth_version", oAuthVersion));
    allParams.addAll(urlParams);

    Collections.sort(allParams, new NvpComparator());

    StringBuffer signatureBaseString3 = new StringBuffer();
    for(int i=0;i<allParams.size();i++)
    {
        NameValuePair nvp = allParams.get(i);
        if (i>0) {
            signatureBaseString3.append("&");
        }
        signatureBaseString3.append(nvp.getName() + "=" + nvp.getValue());
    }

    String signatureBaseStringTemplate = "%s&%s&%s";
    String signatureBaseString =  String.format(signatureBaseStringTemplate, 
                                                                       URLEncoder.encode(signatureBaseString1, "UTF-8"), 
                                                                URLEncoder.encode(signatureBaseString2, "UTF-8"),
                                                                URLEncoder.encode(signatureBaseString3.toString(), "UTF-8"));


    System.out.println("signatureBaseString: "+signatureBaseString);

    String compositeKey = URLEncoder.encode(oAuthConsumerSecret, "UTF-8") + "&" + URLEncoder.encode(oAuthAccessTokenSecret, "UTF-8");

    String oAuthSignature =  computeSignature(signatureBaseString, compositeKey);
    System.out.println("oAuthSignature       : "+oAuthSignature);

    String oAuthSignatureEncoded = URLEncoder.encode(oAuthSignature, "UTF-8");
    System.out.println("oAuthSignatureEncoded: "+oAuthSignatureEncoded);

    String authorizationHeaderValueTempl = "OAuth oauth_consumer_key=\"%s\", oauth_nonce=\"%s\", oauth_signature=\"%s\", oauth_signature_method=\"%s\", oauth_timestamp=\"%s\", oauth_token=\"%s\", oauth_version=\"%s\"";

    String authorizationHeaderValue = String.format(authorizationHeaderValueTempl,
                                                        oAuthConsumerKey,
                                                        oAuthNonce,
                                                        oAuthSignatureEncoded,
                                                        oAuthSignatureMethod,
                                                        oAuthTimestamp,
                                                        oAuthAccessToken,
                                                        oAuthVersion);
    System.out.println("authorizationHeaderValue: "+authorizationHeaderValue);

    StringBuffer urlWithParams = new StringBuffer(url);
    for(int i=0;i<urlParams.size();i++) {
        if(i==0) 
        {
            urlWithParams.append("?");
        }
        else
        {
            urlWithParams.append("&");
        }
        NameValuePair urlParam = urlParams.get(i);
        urlWithParams.append(urlParam.getName() + "=" + urlParam.getValue());
    }

    System.out.println("urlWithParams: "+urlWithParams.toString());
    System.out.println("authorizationHeaderValue:"+authorizationHeaderValue);

    GetMethod getMethod = new GetMethod(urlWithParams.toString());
    getMethod.addRequestHeader("Authorization", authorizationHeaderValue);

    HttpClient cli = new HttpClient();
    int status = cli.executeMethod(getMethod);
    System.out.println("Status:"+status);

    long responseContentLength = getMethod.getResponseContentLength();
    System.out.println("responseContentLength:"+responseContentLength);

    String response = getMethod.getResponseBodyAsString();  
    System.out.println("response: "+response);
}   

错误:

**Cannot instantiate the type NameValuePair**
at line allParams.add(new NameValuePair("oauth_consumer_key", oAuthConsumerKey)); 

**- GetMethod cannot be resolved to a type**
at Line GetMethod getMethod = new GetMethod(urlWithParams.toString());

**- GetMethod cannot be resolved to a type**
at Line HttpClient cli = new HttpClient();

**The method encodeBase64(byte[]) is undefined for the type Base64**
at Line return new String(Base64.encodeBase64(mac.doFinal(text))).trim();
4

1 回答 1

0

保持简单:使用twitter4j提出您的请求,您不需要自己编写;)

于 2013-04-26T08:45:01.070 回答