How to use HTTP POST with “application/octet-stream” in Android? (Microsoft Cognitive Video)

我只是一个虾纸丫 提交于 2019-12-25 08:02:38

问题


I want to use the Video Cognitive Service in Android. The sample that Microsoft provided is used in C#. The video function is sending an URL to the server, So I think it is possible using HTTP POST to send an URL in Android.

http://ppt.cc/V1piA

The problem I met is that I don't know the URL format in "application/octet-stream", and I didn't see the example on the Microsoft website.

Is it possible using HTTP POST in Android to upload a downloaded video to the server, and I can get the analysis result from the server?

If possible, what is the format of the HTTP POST to send request to the server?

Thanks.


回答1:


You may try something like this to send image files for cognitive-services face detect. Using 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());
        }
    }



回答2:


HTTP POST refers to the HTTP method 'POST', application/octet-stream refers to the media type - in this case a stream of application specific octets or bytes.

This is, unfortunately, very subjective as the mechanism for uploading content via HTTP action may be preferred one way or another. Suffice it to say, you will create an InputStream of your content, format a POST request using the mechanism of your choosing:

  • straight Java
  • HTTPClient

Making sure to set the content-type of the POST to application/octet-stream.

After performing the post, consult your API documentation for expected return types.



来源:https://stackoverflow.com/questions/40099028/how-to-use-http-post-with-application-octet-stream-in-android-microsoft-cogn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!