Java 有人使用过这个推特api吗-http://www.twapime.com/
有人试过这个api吗?我在黑莓手机上实现它时遇到了一些问题。推文并不总是发送,我无法访问已发布的推文 这是我的密码-Java 有人使用过这个推特api吗-http://www.twapime.com/,java,blackberry,java-me,Java,Blackberry,Java Me,有人试过这个api吗?我在黑莓手机上实现它时遇到了一些问题。推文并不总是发送,我无法访问已发布的推文 这是我的密码- private void twitterSetup(){ HttpRequest req = new HttpRequest("https://api.twitter.com/oauth/access_token"); req.setMethod(HttpConnection.POST); XAuthSigner signer =
private void twitterSetup(){
HttpRequest req = new HttpRequest("https://api.twitter.com/oauth/access_token");
req.setMethod(HttpConnection.POST);
XAuthSigner signer = new XAuthSigner("", "");
signer.signForAccessToken(req, "", "");
try {
HttpResponse resp = req.send();
if (resp.getCode() == HttpConnection.HTTP_OK)
{
Token accessToken = Token.parse(resp.getBodyContent());
req.close();
req = new HttpRequest("http://api.twitter.com/1/statuses/update.xml");
req.setMethod(HttpConnection.POST);
req.setBodyParameter("status", "new message");
req.setSigner(signer, accessToken);
resp = req.send();
Tweet[] twts = null;
try {
Credential c = new Credential("","","","");
UserAccountManager uam = UserAccountManager.getInstance(c);
List[] lists = null;
ListManager ter = null;
if (uam.verifyCredential()) {
ter = ListManager.getInstance(uam); //pode ser pela classe Timeline tambem.
ListManager listMngr = ListManager.getInstance(uam);
lists = listMngr.getLists();
}
ter.startGetListTweets(lists[0], null, new SearchDeviceListener() {
public void searchCompleted() {}
public void searchFailed(Throwable cause) {}
public void tweetFound(Tweet tweet) {
System.out.println(tweet);
}
});
}
catch(Exception e){
e.printStackTrace();
}
}
else { }
} catch (IOException e) {
e.printStackTrace();
}
catch(Exception e){
}finally {
try {
req.close();
} catch (IOException e) {}
}
}
谢谢你的帮助。好的
下面是im用于根据类型和标记获取twitter内容的类。方法getContent不是最优雅的,但它可以工作。它只是下载并解析一个json文件。
看
公共类GetTwitterContent实现可运行{
私有字符串标签;
私有字符串类型;
公共GetTwitterContent(字符串类型、字符串标记){
this.type=type;
this.tag=tag;
}
公开募捐{
试一试{
Hashtable twitterValuesHashtable=新Hashtable();
字符串serviceUrl=“”;
if(type.equalsIgnoreCase(Constants.TWITTER\u CONTENT\u type\u HASHTAG)){
serviceUrl=Constants.TWITTER\u CONTENT\u HASHTAG\u CONTENT;
}else if(type.equalsIgnoreCase(Constants.TWITTER\u CONTENT\u type\u USER)){
serviceUrl=Constants.TWITTER\u CONTENT\u USER\u CONTENT;
}
ByteArrayOutputStream baos=getContent(serviceUrl+this.tag);
JSONObject JSONObject=newJSONObject(新字符串(baos.toByteArray(),0,baos.size(),“utf-8”);
JSONArray JSONArray=jsonObject.getJSONArray(“结果”);
对于(int计数器=0;计数器
部分工作正常,我试图一条接一条地发送相同的推文。twitter似乎不支持非唯一推文
public class GetTwitterContent implements Runnable {
private String tag;
private String type;
public GetTwitterContent(String type, String tag) {
this.type = type;
this.tag = tag;
}
public void run() {
try {
Hashtable twitterValuesHashtable = new Hashtable();
String serviceUrl = "";
if (type.equalsIgnoreCase(Constants.TWITTER_CONTENT_TYPE_HASHTAG)) {
serviceUrl = Constants.TWITTER_CONTENT_HASHTAG_CONTENT;
} else if (type.equalsIgnoreCase(Constants.TWITTER_CONTENT_TYPE_USER)) {
serviceUrl = Constants.TWITTER_CONTENT_USER_CONTENT;
}
ByteArrayOutputStream baos = getContent(serviceUrl + this.tag);
JSONObject jsonObject = new JSONObject(new String(baos.toByteArray(), 0, baos.size(), "utf-8"));
JSONArray jsonArray = jsonObject.getJSONArray("results");
for (int counter = 0; counter < jsonArray.length(); ++counter) {
JSONObject thisJsonObject = (JSONObject) jsonArray.get(counter);
TwitterResponse twitterResponse = new TwitterResponse();
twitterResponse.setCreatedAt(thisJsonObject.optString("created_at", "na"));
twitterResponse.setTweetText(thisJsonObject.optString("text","na"));
twitterResponse.setFromUser(thisJsonObject.optString("from_user", "na"));
twitterValuesHashtable.put(new Integer(counter),twitterResponse);
}
ServerContent.future.addContent(Constants.TWITTER_KEY, twitterValuesHashtable);
} catch (Exception e) {
e.printStackTrace();
}
}
private ByteArrayOutputStream getContent(String url) {
ByteArrayOutputStream baos = null;
// len = 0;
try {
javax.microedition.io.HttpConnection connection = (javax.microedition.io.HttpConnection) Connector
.open(url);
connection.setRequestMethod(HttpConnection.GET);
// connection.setRequestProperty("Connection", "close");
java.io.InputStream inputStream = connection.openDataInputStream();
// inputStream = getClass().getResourceAsStream(url);
baos = new ByteArrayOutputStream();
int c;
while (true) {
c = inputStream.read();
if (c == -1)
break;
// ++len;
baos.write(c);
}
} catch (Exception e) {
e.printStackTrace();
}
return baos;
}
}