我面临在Android列表视图中集成推文的问题,但我很难在android develomnet中实现或初学者。 我按照这个链接 - > http://therockncoder.blogspot.in/2013/09/android-twitter-api-11-app.html
但是在编辑过程中我遇到了一些错误并且难以删除,并且不知道它是否会起作用。 请帮忙
Logcat详细信息
12-09 18:50:51.762: E/Trace(1786): error opening trace file: No such file or directory (2)
12-09 18:50:51.762: W/Trace(1786): Unexpected value from nativeGetEnabledTags: 0
12-09 18:50:51.762: W/Trace(1786): Unexpected value from nativeGetEnabledTags: 0
12-09 18:50:51.762: W/Trace(1786): Unexpected value from nativeGetEnabledTags: 0
12-09 18:50:52.321: W/Trace(1786): Unexpected value from nativeGetEnabledTags: 0
12-09 18:50:52.321: W/Trace(1786): Unexpected value from nativeGetEnabledTags: 0
12-09 18:50:52.331: D/AndroidRuntime(1786): Shutting down VM
12-09 18:50:52.331: W/dalvikvm(1786): threadid=1: thread exiting with uncaught exception (group=0xb2d17908)
12-09 18:50:52.421: E/AndroidRuntime(1786): FATAL EXCEPTION: main
12-09 18:50:52.421: E/AndroidRuntime(1786): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.eample.twitter_tutorial/com.eample.twitter_tutorial.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.eample.twitter_tutorial.MainActivity" on path: /data/app/com.eample.twitter_tutorial-2.apk
12-09 18:50:52.421: E/AndroidRuntime(1786): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
12-09 18:50:52.421: E/AndroidRuntime(1786): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-09 18:50:52.421: E/AndroidRuntime(1786): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-09 18:50:52.421: E/AndroidRuntime(1786): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-09 18:50:52.421: E/AndroidRuntime(1786): at android.os.Handler.dispatchMessage(Handler.java:99)
12-09 18:50:52.421: E/AndroidRuntime(1786): at android.os.Looper.loop(Looper.java:137)
12-09 18:50:52.421: E/AndroidRuntime(1786): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-09 18:50:52.421: E/AndroidRuntime(1786): at java.lang.reflect.Method.invokeNative(Native Method)
12-09 18:50:52.421: E/AndroidRuntime(1786): at java.lang.reflect.Method.invoke(Method.java:511)
12-09 18:50:52.421: E/AndroidRuntime(1786): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-09 18:50:52.421: E/AndroidRuntime(1786): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-09 18:50:52.421: E/AndroidRuntime(1786): at dalvik.system.NativeStart.main(Native Method)
12-09 18:50:52.421: E/AndroidRuntime(1786): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.eample.twitter_tutorial.MainActivity" on path: /data/app/com.eample.twitter_tutorial-2.apk
12-09 18:50:52.421: E/AndroidRuntime(1786): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
12-09 18:50:52.421: E/AndroidRuntime(1786): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
12-09 18:50:52.421: E/AndroidRuntime(1786): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
12-09 18:50:52.421: E/AndroidRuntime(1786): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
12-09 18:50:52.421: E/AndroidRuntime(1786): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
12-09 18:50:52.421: E/AndroidRuntime(1786): ... 11 more
此处主要活动
public class MainActivity extends ListActivity {
private ListActivity activity;
final static String ScreenName = "therockncoder";
final static String LOG_TAG = "rnc";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity = this;
downloadTweets();
}
// download twitter timeline after first checking to see if there is a network connection
public void downloadTweets() {
ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
new DownloadTwitterTask().execute(ScreenName);
} else {
Log.v(LOG_TAG, "No network connection available.");
}
}
// Uses an AsyncTask to download a Twitter user's timeline
private class DownloadTwitterTask extends AsyncTask<String, Void, String> {
final static String CONSUMER_KEY = "my consumer key";
final static String CONSUMER_SECRET = "my consumer secret";
final static String TwitterTokenURL = "https://api.twitter.com/oauth2/token";
final static String TwitterStreamURL = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=";
@Override
protected String doInBackground(String... screenNames) {
String result = null;
if (screenNames.length > 0) {
result = getTwitterStream(screenNames[0]);
}
return result;
}
// onPostExecute convert the JSON results into a Twitter object (which is an Array list of tweets
@Override
protected void onPostExecute(String result) {
Twitter twits = jsonToTwitter(result);
// lets write the results to the console as well
for (Tweet tweet : twits) {
Log.i(LOG_TAG, tweet.getText());
}
// send the tweets to the adapter for rendering
ArrayAdapter<Tweet> adapter = new ArrayAdapter<Tweet>(activity, android.R.layout.simple_list_item_1, twits);
setListAdapter(adapter);
}
// converts a string of JSON data into a Twitter object
private Twitter jsonToTwitter(String result) {
Twitter twits = null;
if (result != null && result.length() > 0) {
try {
Gson gson = new Gson();
twits = gson.fromJson(result, Twitter.class);
} catch (IllegalStateException ex) {
// just eat the exception
}
}
return twits;
}
// convert a JSON authentication object into an Authenticated object
private Authenticated jsonToAuthenticated(String rawAuthorization) {
Authenticated auth = null;
if (rawAuthorization != null && rawAuthorization.length() > 0) {
try {
Gson gson = new Gson();
auth = gson.fromJson(rawAuthorization, Authenticated.class);
} catch (IllegalStateException ex) {
// just eat the exception
}
}
return auth;
}
private String getResponseBody(HttpRequestBase request) {
StringBuilder sb = new StringBuilder();
try {
DefaultHttpClient httpClient = new DefaultHttpClient(new BasicHttpParams());
HttpResponse response = httpClient.execute(request);
int statusCode = response.getStatusLine().getStatusCode();
String reason = response.getStatusLine().getReasonPhrase();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream inputStream = entity.getContent();
BufferedReader bReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
String line = null;
while ((line = bReader.readLine()) != null) {
sb.append(line);
}
} else {
sb.append(reason);
}
} catch (UnsupportedEncodingException ex) {
} catch (ClientProtocolException ex1) {
} catch (IOException ex2) {
}
return sb.toString();
}
private String getTwitterStream(String screenName) {
String results = null;
// Step 1: Encode consumer key and secret
try {
// URL encode the consumer key and secret
String urlApiKey = URLEncoder.encode(CONSUMER_KEY, "UTF-8");
String urlApiSecret = URLEncoder.encode(CONSUMER_SECRET, "UTF-8");
// Concatenate the encoded consumer key, a colon character, and the
// encoded consumer secret
String combined = urlApiKey + ":" + urlApiSecret;
// Base64 encode the string
String base64Encoded = Base64.encodeToString(combined.getBytes(), Base64.NO_WRAP);
// Step 2: Obtain a bearer token
HttpPost httpPost = new HttpPost(TwitterTokenURL);
httpPost.setHeader("Authorization", "Basic " + base64Encoded);
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
httpPost.setEntity(new StringEntity("grant_type=client_credentials"));
String rawAuthorization = getResponseBody(httpPost);
Authenticated auth = jsonToAuthenticated(rawAuthorization);
// Applications should verify that the value associated with the
// token_type key of the returned object is bearer
if (auth != null && auth.token_type.equals("bearer")) {
// Step 3: Authenticate API requests with bearer token
HttpGet httpGet = new HttpGet(TwitterStreamURL + screenName);
// construct a normal HTTPS request and include an Authorization
// header with the value of Bearer <>
httpGet.setHeader("Authorization", "Bearer " + auth.access_token);
httpGet.setHeader("Content-Type", "application/json");
// update the results with the body of the response
results = getResponseBody(httpGet);
}
} catch (UnsupportedEncodingException ex) {
} catch (IllegalStateException ex1) {
}
return results;
}
}
}
答案 0 :(得分:1)
您需要通过自己的“消费者密钥(API密钥)”和“消费者密钥(API密码)”更改此行
final static String CONSUMER_KEY = "my consumer key";
final static String CONSUMER_SECRET = "my consumer secret";
要执行此操作,请转到https://apps.twitter.com并创建自己的应用