Question

I'm making a google login through GoogleTransport and ClientLogin.

private final GoogleTransport transport = new GoogleTransport();
private final ClientLogin authenticator = new ClientLogin();

Then I'm accessing the Picasa web api.

transport.setVersionHeader(PicasaWebAlbums.VERSION);        
        transport.applicationName = "google-picasaandroidsample-1.0";
        HttpTransport.setLowLevelHttpTransport(ApacheHttpTransport.INSTANCE);
        authenticator.authTokenType = PicasaWebAlbums.AUTH_TOKEN_TYPE;
        authenticator.username = StaticVariables.USER_NAME+StaticVariables.USER_DOMAIN;
        authenticator.password = StaticVariables.USER_PASSWORD;


         try {
            authenticator.authenticate().setAuthorizationHeader(transport);
            HttpRequest request = transport.buildPostRequest();
            request.setUrl("https://picasaweb.google.com/data/feed/api/user/default");
            request.execute();
        } catch (HttpResponseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 

The above is working fine.

Now I want to set a POST request. But buildPostRequest() method does not support any String parameter. So, unable to post any data at the URL. How to achieve it? Please help.

Was it helpful?

Solution

You may use HttpPost with NameValuePair

private boolean sendData(ArrayList<NameValuePair> data) {
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(YOUR_URL);
        httppost.setEntity(new UrlEncodedFormEntity(data));
        HttpResponse response = httpclient.execute(httppost);
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

Then create your Name Value pairs in a different method as

private ArrayList<NameValuePair> setupData() {
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                3);
        nameValuePairs.add(new BasicNameValuePair(USERID, SAMPLE_USER_ID);
        nameValuePairs.add(new BasicNameValuePair(USERNAME, SAMPLE_USER_NAME));
        return nameValuePairs;
    }

Atlast call the send data method in an AsyncTask or Intent service as sendData(setupdata())

OTHER TIPS

Data in a post request is usually sent in the body of the request.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top