0

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
}

}

6
  • 1
    do you have the INTERNET permission set? Commented Mar 11, 2011 at 17:35
  • Yes...!!! I do have it set...!! Commented Mar 11, 2011 at 17:37
  • The Other thing I want to mention is that, I have checked the URL to see if it is malformed. Not that it should hurt ( I should anyway receive an ERROR response). I entered the URL (with the name-value pairs) on the web browser and I receive the 200 OK response from the google server. Commented Mar 11, 2011 at 17:39
  • Yes, I will do that asap. Maybe I should start saving more things on the cloud (the code is in my laptop at home). Commented Mar 11, 2011 at 17:57
  • Might be helpful to show the stack trace for the exception Commented Mar 11, 2011 at 18:39

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.