-2

I am new to scala. I am trying to import contacts from gmail in to my application.I can create sample application in java using Eclipse by following link https://developers.google.com/google-apps/contacts/v2/developers_guide_java?csw=1#retrieving_without_query I can Import the contacts in My java application.And It works fine. My java code is

import com.google.gdata.client.contacts.ContactsService;
import com.google.gdata.data.contacts.ContactEntry;
import com.google.gdata.data.contacts.ContactFeed;
import com.google.gdata.model.gd.Email;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

/**
 * This is a test template
 */

  public class Contacts {

    public static void main(String[] args) {

      try {

        // Create a new Contacts service
          System.out.println("hiiii"+args[0]);
        ContactsService myService = new ContactsService("My Application");
        myService.setUserCredentials(args[0],args[1]);

        // Get a list of all entries
        URL metafeedUrl = new URL("http://www.google.com/m8/feeds/contacts/"+args[0]+"@gmail.com/base");
        System.out.println("Getting Contacts entries...\n");
        ContactFeed resultFeed = myService.getFeed(metafeedUrl, ContactFeed.class);
        List<ContactEntry> entries = resultFeed.getEntries();
        for(int i=0; i<entries.size(); i++) {
          ContactEntry entry = entries.get(i);
          System.out.println("\t" + entry.getTitle().getPlainText());
          System.out.println("\t" + entry.getEmailAddresses());
          for(com.google.gdata.data.extensions.Email emi:entry.getEmailAddresses())
              System.out.println(emi.getAddress());       
        }
        System.out.println("\nTotal Entries: "+entries.size());
      }
      catch(AuthenticationException e) {
        e.printStackTrace();
        System.out.println("Authentication failed");
      }
      catch(MalformedURLException e) {
        e.printStackTrace();
        System.out.println("url");
      }
      catch(ServiceException e) {
        e.printStackTrace();
        System.out.println("Service exc");
      }
      catch(IOException e) {
        e.printStackTrace();
        System.out.println("IO exception");
      }
    }
  }

I tried to use same library functions for My Scala but it doesn't work. My Scala code is

import com.google.gdata.client.contacts.ContactsService
import com.google.gdata.data.contacts.ContactEntry
import com.google.gdata.data.contacts.ContactFeed
import com.google.gdata.util.ServiceException
import com.google.gdata.util.AuthenticationException
import java.io.IOException
import java.net.URL
import java.net.MalformedURLException

object Contacts {
   class Test
   {
   def main(args:Array[String])
   {
      println("hiii")
      try {

        // Create a new Contacts service

        //ContactsService myService = new ContactsService("My Application");
        //myService.setUserCredentials(args[0],args[1]);
        val myService= new ContactsService("My App")
        myService.setUserCredentials("MyemailId","password")
        val metafeedUrl = new URL("http://www.google.com/m8/feeds/contacts/"+"MyemailId"+"@gmail.com/base")
        val resultFeed = myService.getFeed(metafeedUrl, classOf[ContactFeed])
         //List<ContactEntry> entries = resultFeed.getEntries();
              val entries  = resultFeed.getEntries();

              for(i <-0 to entries.size())
              {
                var entry=entries.get(i)
                println(entry.getTitle().getPlainText())
              }
        }
       catch{
         case e:AuthenticationException=>{
           e.printStackTrace();
            }
         case e:MalformedURLException=>{
           e.printStackTrace();
            }
          case e:ServiceException=>{
           e.printStackTrace();
            }
          case e:IOException=>
            {
               e.printStackTrace();
            }
          }


    }

   }
}

But it does not works. Can I use java library in Scala?

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
Prashant Thorat
  • 1,752
  • 11
  • 33
  • 54
  • Please explain by "But not works". What error are you getting, etc.? – Buhake Sindi Sep 05 '13 at 05:23
  • @Buhake Sindi In eclipse it dosn't show any error while typing.But when I tried to run It shows me error that Error Exists in project proceed/cancel. – Prashant Thorat Sep 05 '13 at 05:31
  • 1
    Please provide evidence thereof. We can't help if we cannot see or don't have evidence of what error you're getting. – Buhake Sindi Sep 05 '13 at 05:37
  • @Buhake Sindi I already mention that code works in java but I want in scala. I found plugin for scala gdata-scala-client but it works for scala 2.9 and I am using scala 2.10.So my quetion is can I use same plugin-java in scala? – Prashant Thorat Sep 05 '13 at 05:45

1 Answers1

0

The problem that's causing your error, is that the object Contacts does not have a main method. Instead, it contains an inner class called Test which has a main method. I don't believe that is what you want (in Scala, object methods are the equivalent of Java static methods), so the main method should be moved out into Contacts, and the inner class deleted.

Also, for(i <-0 to entries.size()) is probably a mistake. This is roughly equivalent to for(int i=0; i<=entries.size(); i++) (notice the <=). You probably want for(i <-0 until entries.size()).

While you're there, you can kill the try..catch blocks if you like, as Scala doesn't use checked exceptions. If you import scala.collection.JavaConversions._, then you can use for (entry <- entries), which may be less error prone.

If it still doesn't work (or when posting future questions), provide as much info as you can (error messages, warnings, etc.), as it makes it far more likely that someone will be able to help.

James_pic
  • 3,240
  • 19
  • 24