尝试使用图形api上传视频

时间:2011-04-30 14:20:52

标签: facebook-graph-api

我一直试图为我的移动应用程序实现视频上传Facebook功能一段时间,但从未真正成功休息。我昨天了解到图表替代方案可用。

在获取与访问密钥有关的一些错误后,主要是我已经达到输出流成功写入电影文件的点,输入流只有在我写完3gp文件后才会收到一个空的json数组。

任何人都知道为什么我会得到一个空的json数组,当我得到所有代码运行时没有发布视频,我得到200个响应代码并且服务器发送给我一个非错误响应?

非常感谢任何帮助。

这是获取空白json数组(发送方法)的类。我已将令牌附加到网址和表格中以确定。如果代码不整洁,我很抱歉,这只是一个反复试验的日子。

import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.Enumeration; import java.util.Hashtable; import javax.microedition.io.Connector; import javax.microedition.io.HttpConnection; import net.rim.device.api.io.http.HttpProtocolConstants;

公共类HttpMultipartRequest2 {     static final String BOUNDARY =“---------- V2ymHFg03ehbqgZCaKO6jy”;     byte [] postBytes = null;     String url = null;     Hashtable paramsTable;

public HttpMultipartRequest2(String url, Hashtable params,
        String fileField, String fileName, String fileType, byte[] fileBytes) throws Exception
{
    this.url = url;

    String boundary = getBoundaryString();
    paramsTable = params;
    String boundaryMessage = getBoundaryMessage(boundary, params, fileField, fileName, fileType);
    String endBoundary = "\r\n--" + boundary + "--\r\n";
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bos.write(boundaryMessage.getBytes());
    bos.write(fileBytes);
    bos.write(endBoundary.getBytes());
    this.postBytes = bos.toByteArray();
    bos.close();
}
String getBoundaryString()  {
    return BOUNDARY;
}

String getBoundaryMessage(String boundary, Hashtable params, String fileField, String fileName, String fileType)
{
    StringBuffer res = new StringBuffer("--").append(boundary).append("\r\n");
    Enumeration keys = params.keys();
    while(keys.hasMoreElements())
    {
        String key = (String)keys.nextElement();
        String value = (String)params.get(key);
        res.append("Content-Disposition: form-data; name=\"").append(key).append("\"\r\n")
                .append("\r\n").append(value).append("\r\n")
                .append("--").append(boundary).append("\r\n");
    }
    res.append("Content-Disposition: form-data; name=\"").append(fileField)
            .append("\"; filename=\"").append(fileName).append("\"\r\n")
            .append("Content-Type: ").append(fileType).append("\r\n\r\n");
    Log.info(("res "+res.toString()));
    return res.toString();
}

public String send() throws Exception
{
    StringBuffer sb = new StringBuffer();
    HttpConnection hc = null;
    InputStream is = null;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] res = null;
    try
    {
        Log.info("before hc open"+ url);
        hc = (HttpConnection) Connector.open(url);
        hc.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + getBoundaryString());
        hc.setRequestProperty("access_token", (String)paramsTable.get("access_token"));
        hc.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH, String.valueOf(postBytes.length));
        hc.setRequestProperty( "x-rim-transcode-content", "none" );
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        OutputStream dos = hc.openOutputStream();
         is = hc.openInputStream();
           Log.info("before  dos write responsecode");// + hc.getResponseCode());
        out.write(postBytes, 0, postBytes.length);
         //Log.info("flushing"+ hc.getResponseCode());
        Log.info("after  doswrite responsecode");

              dos.write(out.toByteArray());

              dos.flush();
                     Log.info("after flush");
                    if(dos!=null)
                            dos.close();
        int ch;
         Log.info("before openinput ");

           Log.info("after openinput ");
        while ((ch = is.read()) != -1)
        {
            bos.write(ch);
            sb.append((char)ch);
               Log.info("char"+(char)ch);
        }
        res = bos.toByteArray();
          Log.info("Response recieved from Server is : " + sb.toString() );

    }
    catch(Exception e)
    {
        Log.info(hc.getResponseCode() + "sexce"+e);
    }
    catch(OutOfMemoryError error)
            {
                    Log.info("outofmemory " + error);
                  System.gc();
            }
    finally
    {
        try
        {
            if(bos != null)
                bos.close();
            if(is != null)
                is.close();
            if(hc != null)
                hc.close();
        }
        catch(Exception e2)
        {
           Log.info("finally exception"+ e2);
        }
    }
    return sb.toString();
}

}

1 个答案:

答案 0 :(得分:0)

您是要尝试上传到用户的Feed还是页面?有关页面发布的Open Bug

另外,你可以发布一些代码吗?

假设您已阅读文档:

Facebook Graph API->Video

您使用的是graph-video.facebook.com,而不是graph.facebook.com