如何在Android中使用带有“application / octet-stream”的HTTP POST? (微软认知视频)

问题描述 投票:0回答:2

我想在Android中使用视频认知服务。 Microsoft提供的示例用于C#。视频功能正在向服务器发送URL,因此我认为可以使用HTTP POST在Android中发送URL。

http://ppt.cc/V1piA

我遇到的问题是我不知道“application / octet-stream”中的URL格式,我没有在Microsoft网站上看​​到该示例。

是否可以在Android中使用HTTP POST将下载的视频上传到服务器,我可以从服务器获取分析结果?

如果可能,HTTP POST的格式是什么,以向服务器发送请求?

谢谢。

android microsoft-cognitive
2个回答
1
投票

你可以尝试这样的东西来发送用于认知服务面部检测的图像文件。使用org.apache.httpcomponents :: httpclient:

    @Test
    public void testSendBinary() throws MalformedURLException {
        File picfile = new File("app/sampledata/my_file.jpeg");
        if (!picfile.exists()) throw new AssertionError();


        HttpClient httpclient = HttpClients.createDefault();

        try {
            URIBuilder builder = new URIBuilder("https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect");

            builder.setParameter("returnFaceId", "true");
            builder.setParameter("returnFaceLandmarks", "false");

            URI uri = builder.build();
            HttpPost request = new HttpPost(uri);
            request.setHeader("Content-Type", "application/octet-stream");
            request.setHeader("Ocp-Apim-Subscription-Key", "***");

            // Request body
            request.setEntity(new FileEntity(picfile));

            HttpResponse response = httpclient.execute(request);
            HttpEntity entity = response.getEntity();

            if (entity != null) {
                System.out.println(EntityUtils.toString(entity));
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

0
投票

HTTP POST指的是HTTP method 'POST'application/octet-stream指的是媒体类型 - 在这种情况下是应用程序特定的八位字节或字节流。

遗憾的是,这是非常主观的,因为通过HTTP动作上载内容的机制可能是这样或那样的优选方式。可以这么说,您将创建内容的InputStream,使用您选择的机制格式化POST请求:

确保将POST的内容类型设置为application / octet-stream。

执行帖子后,请参阅API文档以了解预期的返回类型。

© www.soinside.com 2019 - 2024. All rights reserved.