Tuesday, June 5, 2012

Add post to blog through blogger API in Android


Download android-gdata.jar file from [[http://androblogger.googlecode.com/files/android-gdata-1.30.0.jar]] .
Add this jar file to your libs folder and add it to your build path .
Add access internet permission to you manifest file.
<uses-permission android:name="android.permission.INTERNET" />
Authenticating and getting list of user blogs
Create simple layout with getting user credentials and initialize GoogleService object like below.
GoogleService blogger = new GoogleService("blogger",
                    " ");
blogger.setUserCredentials(this.userid, this.password);
Getting BlogsList of user.
Now using initialized googleservice object,you can get list of blognames and its blogids created by given user as below.

private static final String FEED_URI_BASE = "https://www.blogger.com/feeds";//base feed uri
private static final String POSTS_FEED_URI_SUFFIX = "/posts/default";//post uri
final URL feedUrl = new URL(
"http://www.blogger.com/feeds/default/blogs");
Feed resultFeed = blogger.getFeed(feedUrl, Feed.class);//getting list of blogs
Log.e("BloggerAPI", "Size: " + resultFeed.getEntries().size());
if (resultFeed.getEntries().size() > 0) {
for (int i = 0; i < resultFeed.getEntries().size(); i++) {
Entry entry = resultFeed.getEntries().get(i);//getting entry for each blog
String blogId = entry.getId().split("blog-")[1];//splitting blogid from given data
feedUri = FEED_URI_BASE + "/" + blogId;
URL feedUrlnew = new URL(feedUri + POSTS_FEED_URI_SUFFIX);
Feed resultFeednew = myService.getFeed(feedUrlnew,
Feed.class);//getting information for particular blog with blogid
System.out.println(resultFeednew.getTitle().getPlainText());//getting name of blog
blogs.add(resultFeednew.getTitle().getPlainText() + ","
+ blogId);
}
}
After getting blogname and ids,You can add post to any one blog by using blogid with below code.
@Entry entry = new Entry();
entry.setTitle(new PlainTextConstruct(title));//title is title of post entered by user
entry.setContent(new PlainTextConstruct(content));//content of post added by user
try {
            URL postingUrl = new URL("https://www.blogger.com/feeds/"
                    + blogId + "/posts/default");//here blogid is id of blog
            if (blogger.insert(postingUrl, entry) != null) {
                return true;
            } else {
                return false;
            }
        } catch (MalformedURLException mfue) {
            Log.e("", "Malformed post URL used: " + postUrl);
            return false;
        } catch (IOException ioe) {
            Log.e("", "IOException when inserting post. Message:"
                    + ioe.getMessage());
            return false;
        } catch (ServiceException se) {
            Log.e("", "ServiceException when inserting post. Message:"
                    + se.getMessage());
            return false;
        }
    }@

3 comments:

  1. Developing for Android is pretty pleasant: the documentation and tools are good and comprehensive.

    ReplyDelete
  2. The content on your website never confuses me
    blog

    ReplyDelete
  3. hello, do you have a working sample for this implementation?

    ReplyDelete