Hari
Hari

Reputation: 5227

Android application - Google Authorization via HTTP Post

I am just trying to send my Google account authentication data via HTTP POST. I have built the HTTPpost (URLencoded the ArrayList name - value pair) and executed the HTTPClient to get the HTTPResponse. However this is where the problem starts, the HTTPResponse I get back seems to return an exception anytime I try to call one of its associated methods (getStatusLine or getEntity). I also tried to check for "null" response, by doing a simple "if (null) else" type checking, but still no luck.

Is this problem because I am using the emulator?

-----UPDATE-----

I have found out that I am getting a NULL Pointer response, which causes the exception. So, there is an issue with the way I am accessing the Google API. The URL is "https://www.google.com/accounts/ClientLogin" and "Email" and "Passwd" are the two parameters I use for the POST request.

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("<URL HERE>");

try {

    List<NameValuePair> parameters = new ArrayList<NameValuePair>(2);
    parameters.add(<name_value_pair>);
    parameters.add(<name_value_pair>); 
    httppost.setEntity(new UrlEncodedFormEntity(parameters));


    HttpResponse response = httpclient.execute(httppost);

    StatusLine returned_status = response.getStatusLine();
    int status_code = returned_status.getStatusCode();


} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
} catch (IOException e) {
    // TODO Auto-generated catch block
}

}

Upvotes: 0

Views: 1413

Answers (1)

Jazz
Jazz

Reputation:

Instead using httpPost use HttpRequest & also u have to use a library called Base64 & Android version 2.1 &above

    String data;
    HttpParams httpParameters;


   HttpClient client;
    HttpResponse response;
    String userAuth;

    httpParameters = new BasicHttpParams();
                    String auth = android.util.Base64.encodeToString(
                            (username + ":" + userpwd).getBytes("UTF-8"), 
                            android.util.Base64.NO_WRAP
                        );
                        HttpGet request = new HttpGet(StaticURL.uMain+resourceURI);

                        request.addHeader("Authorization", "Basic "+ auth);

                    HttpConnectionParams.setSoTimeout(httpParameters, timeoutConnection);
                    client = new DefaultHttpClient(httpParameters);

                    response = client.execute(request);
                    userAuth = EntityUtils.toString(response.getEntity());

                    System.out.println("Data. in login.."+userAuth);

Upvotes: 2

Related Questions