New to Android, Trying to access google calender through code, always getting IllegalArgument Excptn


Last Updated:

  1. sreedeep.bhu.2007

    sreedeep.bhu.2007 New Member This Topic's Starter

    Joined:
    Oct 22, 2010
    Messages:
    1
    Likes Received:
    0
    Dear All,

    For couple of days I'm trying to access my Google calendar through coding from Android SDK API version 2.2. Though in the Emulator Browser, internet is working fine , webpages are working, but when I'm trying some code, it's giving exception while authenticating. I'm very much new to Android. Please help. Also I'm new to Java. So please ignore bad coding style for now.

    Below is the full code I'm using to authenticate before accessing the calendar:

    package com.androidcode.hello;

    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.UnsupportedEncodingException;
    import java.net.InetAddress;
    import java.net.InetSocketAddress;
    import java.util.ArrayList;
    import java.util.List;

    import org.apache.http.Header;
    import org.apache.http.HeaderIterator;
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpHost;
    import org.apache.http.HttpResponse;
    import org.apache.http.HttpStatus;
    import org.apache.http.NameValuePair;
    import org.apache.http.auth.AuthScope;
    import org.apache.http.auth.UsernamePasswordCredentials;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.conn.params.ConnRoutePNames;
    import org.apache.http.cookie.Cookie;
    import org.apache.http.impl.client.AbstractHttpClient;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.params.BasicHttpParams;
    import org.apache.http.params.HttpConnectionParams;
    import org.apache.http.params.HttpParams;
    import org.apache.http.protocol.BasicHttpContext;
    import org.apache.http.protocol.HTTP;
    import org.apache.http.protocol.HttpContext;

    import android.app.Activity;

    import android.os.Bundle;

    import android.widget.TextView;



    public class HelloWorld extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    //setContentView(R.layout.main);

    TextView tv = new TextView(this);

    String myHeaderVal = sendHttpReq();

    //tv.setText("Hello, Android");
    tv.setText(myHeaderVal);

    setContentView(tv);

    }

    public String sendHttpReq() {

    try{

    String checkHeader = "Authorization";
    String name = "Content-Type";
    String value = "application/x-www-form-urlencoded";
    HttpParams params;
    HttpResponse response;
    String result = "";
    HttpContext localContext;


    System.out.println("Hello");

    HttpParams myParams = new BasicHttpParams();

    HttpConnectionParams.setConnectionTimeout(myParams, 10000);
    HttpConnectionParams.setSoTimeout(myParams, 10000);


    HttpClient httpclient = new DefaultHttpClient(myParams);
    localContext = new BasicHttpContext();

    //for proxy
    setProxy((DefaultHttpClient)httpclient);

    HttpPost httppost = new HttpPost("https://www.google.com/accounts/ClientLogin");
    HttpGet httpget = new HttpGet("http://www.google.com/calendar/feeds/sreedeep.bhu.2007%40gmail.com/private-38d0594193b6ac9e832f4b99c9b807bd/basic?start-min=2010-09-14T13:59:59&start-max=2010-09-22T12:00:01");

    httppost.addHeader(name, value);

    List <NameValuePair> nvps = new ArrayList <NameValuePair>();
    nvps.add(new BasicNameValuePair("Email", "abc@gmail.com"));
    nvps.add(new BasicNameValuePair("Passwd", "abc"));
    nvps.add(new BasicNameValuePair("source", "my-testapp-1"));
    nvps.add(new BasicNameValuePair("service", "cl"));

    try {

    httppost.setEntity(new UrlEncodedFormEntity(nvps , HTTP.UTF_8));

    }
    catch(UnsupportedEncodingException e){

    }

    try {

    System.out.println("before execute");
    response = httpclient.execute(httppost, localContext);
    System.out.println("after execute");


    HttpEntity entity = response.getEntity();

    System.out.println("Login form get: " + response.getStatusLine());
    if (entity != null) {
    //entity.consumeContent();
    }

    if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
    {
    System.out.println("Success");
    }
    else if(response.getStatusLine().getStatusCode() == HttpStatus.SC_FORBIDDEN)
    {
    System.out.println("Forbidden");
    }

    InputStream inputStream = entity.getContent();
    String resp = inputStream.toString();

    // ByteArrayOutputStream content = new ByteArrayOutputStream();
    //
    // // Read response into a buffered stream
    // int readBytes = 0;
    // byte[] sBuffer = null;
    // while ((readBytes = inputStream.read(sBuffer)) != -1) {
    // content.write(sBuffer, 0, readBytes);
    // }

    // String resp = new String(content.toByteArray());
    System.out.println(resp);

    if(response.containsHeader(checkHeader))
    {
    HeaderIterator myHeaderIterator;
    myHeaderIterator = response.headerIterator(checkHeader);

    if(myHeaderIterator.hasNext())
    {
    Header myHeader;
    myHeader = myHeaderIterator.nextHeader();
    String myHeaderVal = myHeader.getValue();
    result = myHeaderVal;
    httpclient.getConnectionManager().shutdown();
    return result;
    }
    }

    }
    catch(ClientProtocolException e)
    {

    }


    /*

    HttpPost httpost = new HttpPost("https://portal.sun.com/amserver/UI/Login?" +
    "org=self_registered_users&" +
    "goto=/portal/dt&" +
    "gotoOnFail=/portal/dt?error=true");

    List <NameValuePair> nvps = new ArrayList <NameValuePair>();
    nvps.add(new BasicNameValuePair("IDToken1", "username"));
    nvps.add(new BasicNameValuePair("IDToken2", "password"));

    httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

    response = httpclient.execute(httpost);
    entity = response.getEntity();

    System.out.println("Login form get: " + response.getStatusLine());
    if (entity != null) {
    entity.consumeContent();
    }

    System.out.println("Post logon cookies:");
    cookies = httpclient.getCookieStore().getCookies();
    if (cookies.isEmpty()) {
    System.out.println("None");
    } else {
    for (int i = 0; i < cookies.size(); i++) {
    System.out.println("- " + cookies.get(i).toString());
    }
    }
    */
    // When HttpClient instance is no longer needed,
    // shut down the connection manager to ensure
    // immediate deallocation of all system resources
    httpclient.getConnectionManager().shutdown();
    return result;

    }
    catch(Exception e)
    {
    e.printStackTrace();
    System.out.println(e.getLocalizedMessage());
    System.out.println(e.getMessage());
    }
    return null;

    }

    public void setProxy(DefaultHttpClient httpclient) {
    final String PROXY_IP = "107.108.85.10";
    final int PROXY_PORT = 80;

    httpclient.getCredentialsProvider().setCredentials(
    new AuthScope(PROXY_IP, PROXY_PORT),
    new UsernamePasswordCredentials(
    "", ""));

    HttpHost proxy = new HttpHost(PROXY_IP, PROXY_PORT);

    httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
    proxy);


    }
    }



    And the exception stack print I'm getting is -

    10-22 10:50:17.692: WARN/System.err(309): java.lang.IllegalArgumentException
    10-22 10:50:17.722: WARN/System.err(309): at java.net.InetSocketAddress.<init>(InetSocketAddress.java:98)
    10-22 10:50:17.732: WARN/System.err(309): at java.net.InetSocketAddress.<init>(InetSocketAddress.java:89)
    10-22 10:50:17.742: WARN/System.err(309): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.<init>(OpenSSLSocketImpl.java:238)
    10-22 10:50:17.753: WARN/System.err(309): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImplWrapper.<init>(OpenSSLSocketImplWrapper.java:35)
    10-22 10:50:17.763: WARN/System.err(309): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketFactoryImpl.createSocket(OpenSSLSocketFactoryImpl.java:83)
    10-22 10:50:17.773: WARN/System.err(309): at org.apache.http.conn.ssl.SSLSocketFactory.createSocket(SSLSocketFactory.java:375)
    10-22 10:50:17.783: WARN/System.err(309): at org.apache.http.impl.conn.DefaultClientConnectionOperator.updateSecureConnection(DefaultClientConnectionOperator.java:225)
    10-22 10:50:17.793: WARN/System.err(309): at org.apache.http.impl.conn.AbstractPoolEntry.layerProtocol(AbstractPoolEntry.java:302)
    10-22 10:50:17.793: WARN/System.err(309): at org.apache.http.impl.conn.AbstractPooledConnAdapter.layerProtocol(AbstractPooledConnAdapter.java:146)
    10-22 10:50:17.813: WARN/System.err(309): at org.apache.http.impl.client.DefaultRequestDirector.establishRoute(DefaultRequestDirector.java:623)
    10-22 10:50:17.823: WARN/System.err(309): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:352)
    10-22 10:50:17.833: WARN/System.err(309): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
    10-22 10:50:17.852: WARN/System.err(309): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
    10-22 10:50:17.852: WARN/System.err(309): at com.androidcode.hello.HelloWorld.sendHttpReq(HelloWorld.java:119)
    10-22 10:50:17.862: WARN/System.err(309): at com.androidcode.hello.HelloWorld.onCreate(HelloWorld.java:60)
    10-22 10:50:17.873: WARN/System.err(309): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    10-22 10:50:17.884: WARN/System.err(309): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
    10-22 10:50:17.884: WARN/System.err(309): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    10-22 10:50:17.903: WARN/System.err(309): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
    10-22 10:50:17.903: WARN/System.err(309): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
    10-22 10:50:17.922: WARN/System.err(309): at android.os.Handler.dispatchMessage(Handler.java:99)
    10-22 10:50:17.922: WARN/System.err(309): at android.os.Looper.loop(Looper.java:123)
    10-22 10:50:17.932: WARN/System.err(309): at android.app.ActivityThread.main(ActivityThread.java:4627)
    10-22 10:50:17.942: WARN/System.err(309): at java.lang.reflect.Method.invokeNative(Native Method)
    10-22 10:50:17.952: WARN/System.err(309): at java.lang.reflect.Method.invoke(Method.java:521)
    10-22 10:50:17.962: WARN/System.err(309): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    10-22 10:50:17.974: WARN/System.err(309): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    10-22 10:50:17.983: WARN/System.err(309): at dalvik.system.NativeStart.main(Native Method)
    10-22 10:51:24.892: ERROR/CheckinTask(231): Checkin failed: https://android.clients.google.com/checkin (request #0)
    10-22 10:51:24.892: ERROR/CheckinTask(231): org.apache.http.conn.ConnectTimeoutException: Connect to /64.233.183.100:443 timed out
    10-22 10:51:24.892: ERROR/CheckinTask(231): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:121)
    10-22 10:51:24.892: ERROR/CheckinTask(231): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:143)
    10-22 10:51:24.892: ERROR/CheckinTask(231): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
    10-22 10:51:24.892: ERROR/CheckinTask(231): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
    10-22 10:51:24.892: ERROR/CheckinTask(231): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:348)
    10-22 10:51:24.892: ERROR/CheckinTask(231): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
    10-22 10:51:24.892: ERROR/CheckinTask(231): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
    10-22 10:51:24.892: ERROR/CheckinTask(231): at android.net.http.AndroidHttpClient.execute(AndroidHttpClient.java:248)
    10-22 10:51:24.892: ERROR/CheckinTask(231): at com.google.android.common.http.GoogleHttpClient.executeWithoutRewriting(GoogleHttpClient.java:203)
    10-22 10:51:24.892: ERROR/CheckinTask(231): at com.google.android.common.http.GoogleHttpClient.execute(GoogleHttpClient.java:245)
    10-22 10:51:24.892: ERROR/CheckinTask(231): at com.google.android.common.http.GoogleHttpClient.execute(GoogleHttpClient.java:313)
    10-22 10:51:24.892: ERROR/CheckinTask(231): at com.google.android.gsf.checkin.CheckinTask.sendRequest(CheckinTask.java:239)
    10-22 10:51:24.892: ERROR/CheckinTask(231): at com.google.android.gsf.checkin.CheckinTask.doInBackground(CheckinTask.java:146)
    10-22 10:51:24.892: ERROR/CheckinTask(231): at com.google.android.gsf.checkin.CheckinService$1.doInBackground(CheckinService.java:221)
    10-22 10:51:24.892: ERROR/CheckinTask(231): at com.google.android.gsf.checkin.CheckinService$1.doInBackground(CheckinService.java:228)
    10-22 10:51:24.892: ERROR/CheckinTask(231): at android.os.AsyncTask$2.call(AsyncTask.java:185)
    10-22 10:51:24.892: ERROR/CheckinTask(231): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
    10-22 10:51:24.892: ERROR/CheckinTask(231): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
    10-22 10:51:24.892: ERROR/CheckinTask(231): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
    10-22 10:51:24.892: ERROR/CheckinTask(231): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
    10-22 10:51:24.892: ERROR/CheckinTask(231): at java.lang.Thread.run(Thread.java:1096)
     

    Advertisement

Share This Page

Loading...