Search code examples
androidfoursquare

Foursquare check-in


I am working in Android. I have done almost all the work which is required for Foursquare integration in an Android application.

Now I am trying to add check in functionality in this application.

This is my code in android for foursquare check-in :

URL url = new URL("https://api.foursquare.com/v2/checkins/add?venueId=4d6a73bafd7ea35d0c08b24a&shout=great....&broadcast=public&oauth_token=myauthtoken");
URLConnection conn = url.openConnection();

Please tell me whether I am doing right or not? Actually I dont know that what is the procedure to add check in functionality for a venue through Foursquare in our android application.


Solution

  • import java.io.IOException;
    import java.io.InputStream;
    import java.io.UnsupportedEncodingException;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.List;
    
    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    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.client.params.ClientPNames;
    import org.apache.http.client.params.CookiePolicy;
    import org.apache.http.client.utils.URLEncodedUtils;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.DefaultHttpClient;
    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.HttpContext;
    import org.apache.http.util.EntityUtils;
    
    DefaultHttpClient httpClient;
    HttpContext localContext;
    private String ret;
    HttpResponse response = null;
    HttpPost httpPost = null;
    
    public String sendPost(String url, List<NameValuePair> data, String contentType) {
        ret = null;
    
        httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);
    
        httpPost = new HttpPost(url);
        response = null;
    
        StringEntity tmp = null;
    
        httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.215 Safari/535.1");
        httpPost.setHeader("Accept", "text/html,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
    
        if (contentType != null) {
            httpPost.setHeader("Content-Type", contentType);
        } else {
            httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
        }
    
        try {
            tmp = new UrlEncodedFormEntity(data);
        } catch (UnsupportedEncodingException e) {
    
        }
    
        httpPost.setEntity(tmp);
        response = httpClient.execute(httpPost, localContext);
    
    
        if (response != null) {
            ret = EntityUtils.toString(response.getEntity());
        }
    
    
        return ret;
    }
    

    Try using this method to post your checkin url with an empty ArrayList and null contentType.