我在Android上工作。我已经完成了在Android应用程序中集成Foursquare所需的几乎所有工作。
现在,我正尝试在此应用程序中添加签入功能。
这是我在android中为foursquare签到的代码:
URL url = new URL("https://api.foursquare.com/v2/checkins/add?venueId=4d6a73bafd7ea35d0c08b24a&shout=great....&broadcast=public&oauth_token=myauthtoken");
URLConnection conn = url.openConnection();请告诉我我做得对不对?实际上,我不知道在我们的android应用程序中通过Foursquare为一个会场添加签到功能的步骤是什么。
发布于 2011-12-29 21:22:06
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;
}尝试使用此方法发布带有空ArrayList和空contentType的签入url。
发布于 2011-12-30 12:13:34
我是这样解决我的问题的:
try {
HttpPost post = new
HttpPost("https://api.foursquare.com/v2/checkins/add?venueId="+mNearbyList.get(position).id+"&shout=great....&broadcast=public&oauth_token="mytoken");
HttpClient hc=new DefaultHttpClient();
HttpResponse rp = hc.execute(post);
// Log.v(TAG,"response from server "+EntityUtils.toString(rp.getEntity()));
if (rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
Toast.makeText(PlacetoCheckin.this, "Done", Toast.LENGTH_SHORT).show();
String response = EntityUtils
.toString(rp.getEntity());
Log.v(TAG,
"response from server====="
+ response);
}
}
catch(Exception e){}
}
});发布于 2011-12-29 19:46:25
谷歌代码上有一些旧的安卓代码,这些代码是由FourSquare为一个安卓FourSquare应用程序编写的。也许你可以在那里找到一些例子。
这是链接:http://code.google.com/p/foursquared/source/browse/#hg
https://stackoverflow.com/questions/8667408
复制相似问题