How can I publish a location checkin on facebook from Android?
This is my attempt to publish checkins from my Android app:
public String APP_ID = "[redacted]";
Facebook authenticatedFacebook = new Facebook(APP_ID);
AsyncFacebookRunner fbinvoker = new AsyncFacebookRunner(authenticatedFacebook);
Bundle params = new Bundle();
params.putByteArray("access_token", Main.access.getBytes());
params.putByteArray("message", "Hi".getBytes());
params.putByteArray("place", Main.place_id.getBytes());
JSONObject jo = new JSONObject();
JSONObject frnd_data = new JSONObject();
try {
jo.put("latitude", lat_value);
jo.put("longitude", long_value);
params.putByteArray("coordinates", jo.toString().getBytes());
frnd_data.put("USER_ID1", "100000838520166");
params.putByteArray("tags", frnd_data.toString().getBytes());
fbinvoker.request("https://graph.facebook.com/me/checkins", params, "POST", null, null);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
and here's the openUrl()
method:
public static String openUrl(String url, String method, Bundle params)
throws MalformedURLException, IOException {
// random string as boundary for multi-part http post
String strBoundary = "3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f";
String endLine = "\r\n";
OutputStream os;
if (method.equals("GET")) {
url = url + "?" + encodeUrl(params);
}
Log.d("Facebook-Util", method + " URL: " + url);
HttpURLConnection conn =
(HttpURLConnection) new URL(url).openConnection();
conn.setRequestProperty("User-Agent", System.getProperties().
getProperty("http.agent") + " FacebookAndroidSDK");
if (!method.equals("GET")) {
Bundle dataparams = new Bundle();
for (String key : params.keySet()) {
if (params.getByteArray(key) != null) {
dataparams.putByteArray(key, params.getByteArray(key));
}
}
// use method override
if (!params.containsKey("method")) {
params.putString("method", method);
}
if (params.containsKey("access_token")) {
String decoded_token =
URLDecoder.decode(params.getString("access_token"));
params.putString("access_token", decoded_token);
}
conn.setRequestMethod("POST");
conn.setRequestProperty(
"Content-Type",
"multipart/form-data;boundary="+strBoundary);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestProperty("Connection", "Keep-Alive");
conn.connect();
os = new BufferedOutputStream(conn.getOutputStream());
os.write(("--" + strBoundary +endLine).getBytes());
os.write((encodePostBody(params, strBoundary)).getBytes());
os.write((endLine + "--" + strBoundary + endLine).getBytes());
if (!dataparams.isEmpty()) {
for (String key: dataparams.keySet()){
os.write(("Content-Disposition: form-data; filename=\"" + key + "\"" + endLine).getBytes());
os.write(("Content-Type: content/unknown" + endLine + endLine).getBytes());
os.write(dataparams.getByteArray(key));
os.write((endLine + "--" + strBoundary + endLine).getBytes());
}
}
os.flush();
}
String response = "";
try {
response = read(conn.getInputStream());
} catch (FileNotFoundException e) {
// Error Stream contains JSON that we can parse to a FB error
response = read(conn.getErrorStream());
}
return response;
}
This is the format I got from facebook's SDK:
curl -F 'access_token=...' \
-F 'message=The coffee is just meh.' \ -F 'place=PAGE_ID' \ -F 'coordinates={"latitude":"...", "longitude": "..."}' \ -F 'tags=USER_ID1, USER_ID2' \ https://graph.facebook.com/me/checkins
Now I'm getting these errors:
03-30 19:50:23.621: WARN/Bundle(13255): Key format expected byte[] but value was a java.lang.String. The default value was returned.
03-30 19:50:23.651: WARN/Bundle(13255): Attempt to cast generated internal exception: 03-30 19:50:23.651: WARN/Bundle(13255): java.lang.ClassCastException: java.lang.String 03-30 19:50:23.651: WARN/Bundle(13255): at android.os.Bundle.getByteArray(Bun开发者_运维百科dle.java:1220) 03-30 19:50:23.651: WARN/Bundle(13255): at com.facebook.android.Util.openUrl(Util.java:155) 03-30 19:50:23.651: WARN/Bundle(13255): at com.facebook.android.Facebook.request(Facebook.java:559) 03-30 19:50:23.651: WARN/Bundle(13255): at com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:253)
[snipped for length; hundreds more lines of error output are in the revision history]
I finally figured out the issue. Just ignore these errors. You can publish checkins as I show below:
Bundle params = new Bundle();
params.putString("access_token", Main.access);
params.putString("place", "203682879660695");
params.putString("Message","I m here in this place");
JSONObject coordinates = new JSONObject();
coordinates.put("latitude", Main.mylat);
coordinates.put("longitude", Main.mylong);
params.putString("coordinates",coordinates.toString());
JSONArray frnd_data=new JSONArray();
params.putString("tags", "xxxx");//where xx indicates the User Id
String response = faceBook.request("me/checkins", params, "POST");
Log.d("Response",response);
There's no need to use putByteArray()
instead of putString()
.
, where as instead of calling AsyncFacebookRunner
you have to call FaceBook object . request, just call the method
This example doesn`t work for me till i replaced this:
JSONObject coordinates = new JSONObject();
coordinates.put("latitude", Main.mylat);
coordinates.put("longitude", Main.mylong);
params.putString("coordinates",coordinates.toString());
to this:
params.putString("coordinates", "{\"longitude\":\"123.123\",\"latitude\":\"123.123\"}");
精彩评论