如何在Android上发布Facebook登录位置?

时间:2011-03-30 17:44:26

标签: android facebook

这是我尝试从我的Android应用发布签名:

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();
}

这是openUrl()方法:

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;
}

这是我从facebook的SDK获得的格式:

  

curl -F'access_token = ...'\\        -F'消息=咖啡只是meh。' \
       -F'place = PAGE_ID'\
       -F'shithions = {“纬度”:“......”,
  “经度”:“......”}'\ n        -F'tag = USER_ID1,USER_ID2'\
       https://graph.facebook.com/me/checkins

现在我收到了这些错误:

  

03-30 19:50:23.621:WARN / Bundle(13255):键格式预期byte []但值是java.lang.String。返回了默认值   03-30 19:50:23.651:WARN / Bundle(13255):尝试转换生成的内部异常:
  03-30 19:50:23.651:WARN / Bundle(13255):java.lang.ClassCastException:java.lang.String
  03-30 19:50:23.651:WARN / Bundle(13255):在android.os.Bundle.getByteArray(Bundle.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)

[剪了一下;在the revision history]

中有数百行错误输出

2 个答案:

答案 0 :(得分:3)

我终于弄明白了这个问题。只是忽略这些错误。您可以发布签名,如下所示:

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);

无需使用putByteArray()代替putString()。 ,而不是调用AsyncFacebookRunner,你必须调用FaceBook对象。请求,只需调用方法

答案 1 :(得分:0)

这个例子对我来说不起作用,直到我取代它:

JSONObject coordinates = new JSONObject();
coordinates.put("latitude", Main.mylat);
coordinates.put("longitude", Main.mylong);
params.putString("coordinates",coordinates.toString());

到此:

params.putString("coordinates", "{\"longitude\":\"123.123\",\"latitude\":\"123.123\"}");
相关问题