Actually, I am facing a problem in Android. I am creating an application that connects to a Twitter like app called Status.Net. I have created an account in this site with my own username and password. Now the application which I have created, connects to this Status.Net site. Originally, this android application was picked up from one of the learning android website called Learning Android by Marko Gargenta and I have downloaded the source code from its git. I am creating my own version of this application by specifying a different API root as farmnews.aaditech.com/api (originally the API root was http://yamba.marakana.com/api). The code with original API root was working fine. It updated the status in yamba.marakana.com and the status is shown in the timeline too. But when I specify my own API root, it just updates into the status.net and shows a toast message “Failed To Post” which should not be bcoz it is obviously posting on the status.net. I don’t knw wht is the problem. But I know if I just change one line of the API root, everything will be working fine.
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
I tried using the other API root URL http://identi.ca/api but it does not seem to be working
Please help me out
Regards and Thanks
Sohaib Rahman
I’ve had the same problem, the identi.ca API doesn’t appear to return values for:
profile_background_color
profile_link_color
profile_text_color
profile_sidebar_fill_color
profile_background_tile
But the customised JTwitter library supplied by the book’s author expects the API to return values for them and when it doesn’t find them, it throws a org.json.JSONException wrapped up in a winterwell.jtwitter.TwitterException.
I’ve downloaded the source for the library from:
https://github.com/marakana/LearningAndroidYamba/tree/master/JTwitterYamba
and updated the library by commenting out the relevant lines for those items listed above. This has solved the problem for me.