gpt4 book ai didi

java - Android Twitter异常错误:(

转载 作者:行者123 更新时间:2023-12-01 16:03:40 24 4
gpt4 key购买 nike

开始编写一个简单的 Android 应用程序。其中一部分会提取用户最近 20 条推文并将其保存到数据库中。当我运行这个时,statuses = twitter.getUserTimeline();抛出一个已处理的异常并打印出:无法获得时间线权限被拒绝。当我在非 Android 系统上运行相同的代码时,它工作得很好。我有点困惑。 android 会限制网络连接吗?我使用一些数据库帮助器类,没有包含代码,因为在涉及其中任何一个之前就抛出异常:(以下是代码:

import twitter4j.Paging;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;


import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class TwitterTest extends ListActivity{
private ArrayList<String> queryString;
private DatabaseQuery query;






/* returns date in string format-without time */
String getThisYearDate(Date d){
String x = d.toString();
String y= x.substring(0, 11);
String z =x.substring(17);
String a = y.concat(z);
return a;
}

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
query = new DatabaseQuery(this);

/*If file exists then tables in db created successfully*/

File file = new File("exists.txt");




boolean success = file.exists();

if (success==false) {
try {

ArrayList<Status> statuses = new ArrayList<Status>();


Twitter twitter = new TwitterFactory().getInstance("USERNAME", "PASSWORD");



statuses = twitter.getUserTimeline();

/* adds status data to database*/
for(Status s: statuses){

query.appendData("Text", s.getText());
query.appendData("Date", this.getThisYearDate(s.getCreatedAt()));
query.appendData("ID",String.valueOf(s.getId()) );
query.addRow();
}



}
catch (TwitterException te) {
System.out.println("Failed to get timeline: " + te.getMessage());

System.exit( -1);
}
try{
boolean created = file.createNewFile();
}
catch (IOException e) {

System.out.println("error");




}

queryString = query.getData(new String[] {"Text"},this.getThisYearDate(new Date()), null, null, null, "Date", " ASC");
try {
query.destroy();
} catch (Throwable e) {
e.printStackTrace();
}

} else {
// File already exists
queryString = query.getData(new String[] {"Text"},this.getThisYearDate(new Date()), null, null, null, "Date", " ASC");
try {
query.destroy();
} catch (Throwable e) {
e.printStackTrace();
}

}





// Set the ListView
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, queryString));
getListView().setTextFilterEnabled(true);
}
}

最佳答案

使用异步任务,它对我有用

public class Asy  extends AsyncTask<String, String, String> { 
static String msg ;
@Override
protected void onPreExecute() {
super.onPreExecute();

}


protected String doInBackground(String... args) {

android.tweet(msg);
return null;
}


protected void onPostExecute(String file_url) {


}
}

其中 android 是包含您的代码的类:

public class android {
static boolean got = true;
public static String consumerKey = "************";

public static String consumerSecret = "***********";

public final static String accesstoken = "***************";

public final static String accesstokensecret = "**************";

private final String CALLBACKURL = "T4J_OAuth://callback_main";

public static void tweet(String s) {

ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthAccessToken(accesstoken);
builder.setOAuthAccessTokenSecret(accesstokensecret);
builder.setOAuthConsumerKey(consumerKey);
builder.setOAuthConsumerSecret(consumerSecret);
OAuthAuthorization auth = new OAuthAuthorization(builder.build());
Twitter twitter = new TwitterFactory().getInstance(auth);

try {
twitter.updateStatus(s); //I was trying to tweet
//put whatever you want to do
} catch (TwitterException e) {

return;
}

}

然后使用以下代码调用此异步任务:

Asy.msg = "whatever";      // the status I was posting
Asy task3 = new Asy();
task3.execute();`

这对我有用。祝你好运

关于java - Android Twitter异常错误:(,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3165553/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com