实际上,我在Android中遇到了问题。我正在创建一个连接到Twitter的应用程序,如应用程序Status.Net。我使用自己的用户名和密码在此站点中创建了一个帐户。现在我创建的应用程序连接到此Status.Net站点。最初,这个Android应用程序是从Marko Gargenta的一个名为Learning Android的学习Android网站中挑选出来的,我已经从它的git下载了源代码。我通过将不同的API根目录指定为farmnews.aaditech.com/api(最初API根为http://yamba.marakana.com/api)来创建我自己的此应用程序版本。原始API root的代码工作正常。它更新了yamba.marakana.com中的状态,状态也显示在时间线中。但是当我指定自己的API根目录时,它只是更新到status.net并显示一个toast消息“Failed To Post”,它不应该是bcoz,它显然是在status.net上发布的。我不知道是什么问题。但我知道如果我只改变API根的一行,一切都会正常工作。
YambaApplication.java
package com.marakana.yamba8;
import java.util.List;
import winterwell.jtwitter.Twitter;
import winterwell.jtwitter.Twitter.Status;
import android.app.Application;
import android.content.ContentValues;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;
public class YambaApplication extends Application implements
OnSharedPreferenceChangeListener {
private static final String TAG = YambaApplication.class.getSimpleName();
public static final String LOCATION_PROVIDER_NONE = "NONE";
public static final long INTERVAL_NEVER = 0;
public Twitter twitter;
private SharedPreferences prefs;
private StatusData statusData;
private boolean serviceRunning;
private boolean inTimeline;
@Override
public void onCreate() {
super.onCreate();
this.prefs = PreferenceManager.getDefaultSharedPreferences(this);
this.prefs.registerOnSharedPreferenceChangeListener(this);
this.statusData = new StatusData(this);
Log.i(TAG, "Application started");
}
public synchronized Twitter getTwitter() {
if (this.twitter == null) {
String username = this.prefs.getString("username", null);
String password = this.prefs.getString("password", null);
//String url = this.prefs.getString("url",
// "http://yamba.marakana.com/api");
String url = this.prefs.getString("url",
"http://farmnews.aaditech.com/index.php/api"); //1
//String url = this.prefs.getString("url",
//"http://identi.ca/api");
//String url = this.prefs.getString("url",
//"http://farmnews.aaditech.com");
if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password)
&& !TextUtils.isEmpty(url)) {
this.twitter = new Twitter(username, password);
this.twitter.setAPIRootUrl(url);
}
}
return this.twitter;
}
public boolean startOnBoot() {
return this.prefs.getBoolean("startOnBoot", false);
}
public StatusData getStatusData() {
return statusData;
}
public synchronized int fetchStatusUpdates() {
Log.d(TAG, "Fetching status updates");
Twitter twitter = this.getTwitter();
if (twitter == null) {
Log.d(TAG, "Twitter connection info not initialized");
return 0;
}
try {
List<Status> statusUpdates = twitter.getFriendsTimeline();
long latestStatusCreatedAtTime = this.getStatusData()
.getLatestStatusCreatedAtTime();
int count = 0;
ContentValues values = new ContentValues();
for (Status status : statusUpdates) {
values.put(StatusData.C_ID, status.getId());
long createdAt = status.getCreatedAt().getTime();
values.put(StatusData.C_CREATED_AT, createdAt);
values.put(StatusData.C_TEXT, status.getText());
values.put(StatusData.C_USER, status.getUser().getName());
Log.d(TAG, "Got update with id " + status.getId() + ". Saving");
this.getStatusData().insertOrIgnore(values);
if (latestStatusCreatedAtTime < createdAt) {
count++;
}
}
Log.d(TAG, count > 0 ? "Got " + count + " status updates"
: "No new status updates");
return count;
} catch (Exception e) {
Log.e(TAG, "Failed to fetch status updates", e);
return 0;
}
}
public synchronized void onSharedPreferenceChanged(
SharedPreferences sharedPreferences, String key) {
this.twitter = null;
}
public boolean isServiceRunning() {
return serviceRunning;
}
public void setServiceRunning(boolean serviceRunning) {
this.serviceRunning = serviceRunning;
}
public boolean isInTimeline() {
return inTimeline;
}
public void setInTimeline(boolean inTimeline) {
this.inTimeline = inTimeline;
}
public String getProvider() {
return prefs.getString("provider", LOCATION_PROVIDER_NONE);
}
public long getInterval() {
// For some reason storing interval as long doesn't work
return Long.parseLong(prefs.getString("interval", "0"));
}
@Override
public void onTerminate() {
super.onTerminate();
this.statusData.close();
Log.i(TAG, "Application terminated");
}
}
DDMS LogCat
10-28 00:36:04.964: ERROR/YambaApplication(844): Failed to fetch status updates
10-28 00:36:04.964: ERROR/YambaApplication(844): winterwell.jtwitter.TwitterException: org.json.JSONException: No value for profile_background_color
10-28 00:36:04.964: ERROR/YambaApplication(844): at winterwell.jtwitter.Twitter$User.<init>(Twitter.java:693)
10-28 00:36:04.964: ERROR/YambaApplication(844): at winterwell.jtwitter.Twitter$Status.<init>(Twitter.java:464)
10-28 00:36:04.964: ERROR/YambaApplication(844): at winterwell.jtwitter.Twitter$Status.getStatuses(Twitter.java:342)
10-28 00:36:04.964: ERROR/YambaApplication(844): at winterwell.jtwitter.Twitter.getStatuses(Twitter.java:1691)
10-28 00:36:04.964: ERROR/YambaApplication(844): at winterwell.jtwitter.Twitter.getFriendsTimeline(Twitter.java:1454)
10-28 00:36:04.964: ERROR/YambaApplication(844): at com.marakana.yamba8.YambaApplication.fetchStatusUpdates(YambaApplication.java:73)
10-28 00:36:04.964: ERROR/YambaApplication(844): at com.marakana.yamba8.UpdaterService.onHandleIntent(UpdaterService.java:35)
10-28 00:36:04.964: ERROR/YambaApplication(844): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59)
10-28 00:36:04.964: ERROR/YambaApplication(844): at android.os.Handler.dispatchMessage(Handler.java:99)
10-28 00:36:04.964: ERROR/YambaApplication(844): at android.os.Looper.loop(Looper.java:123)
10-28 00:36:04.964: ERROR/YambaApplication(844): at android.os.HandlerThread.run(HandlerThread.java:60)
10-28 00:36:04.964: ERROR/YambaApplication(844): Caused by: org.json.JSONException: No value for profile_background_color
10-28 00:36:04.964: ERROR/YambaApplication(844): at org.json.JSONObject.get(JSONObject.java:354)
10-28 00:36:04.964: ERROR/YambaApplication(844): at org.json.JSONObject.getString(JSONObject.java:510)
10-28 00:36:04.964: ERROR/YambaApplication(844): at winterwell.jtwitter.Twitter$User.<init>(Twitter.java:660)
10-28 00:36:04.964: ERROR/YambaApplication(844): ... 10 more
我尝试使用其他API根网址http://identi.ca/api,但似乎没有工作
请帮帮我
问候和谢谢
Sohaib Rahman
答案 0 :(得分:2)
我遇到了同样的问题,identi.ca API似乎没有返回以下值:
profile_background_color
profile_link_color
profile_text_color
profile_sidebar_fill_color
profile_background_tile
但本书作者提供的自定义JTwitter库希望API为它们返回值,当它找不到它们时,它会抛出一个org.json.JSONException,它包含在winterwell.jtwitter.TwitterException中。
我从以下网址下载了该库的来源:
https://github.com/marakana/LearningAndroidYamba/tree/master/JTwitterYamba
并通过注释掉上面列出的项目的相关行来更新库。这解决了我的问题。