带有原始图像数据的Volley POST请求

时间:2019-03-12 17:14:26

标签: android azure post android-volley

我正在尝试使用Volley使用POST请求调用Azure计算机视觉REST API,以上传要分析的图像。

以下是API文档:https://southeastasia.dev.cognitive.microsoft.com/docs/services/5adf991815e1060e6355ad44/operations/56f91f2e778daf14a499e1fa

输入在POST正文中传递,我想使用application/octet-stream发送原始图像二进制文件。

我使用bitmap.compress(...)byteArrayOutputStream.toByteArray();将图像转换为原始二进制文件

到目前为止,我在发送原始二进制图像数据方面没有成功,它给出了错误400。

代码如下:

public void testVollReq(){

   final TextView tv = findViewById(R.id.tv);
   String url = "https://southeastasia.api.cognitive.microsoft.com/vision/v1.0/describe";
   final StringBuilder stringBuilder = new StringBuilder();

   JSONObject postParams = new JSONObject();
   try {
       postParams.put("maxCandidates","1");
   } catch (JSONException e) {
       e.printStackTrace();
   }

   JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
        Request.Method.POST,
        url,
        null,
        new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    JSONObject descriptionObject = response.getJSONObject("description");
                    JSONArray captionsArr = descriptionObject.getJSONArray("captions");
                    for (int i = 0; i < captionsArr.length(); i++) {
                        JSONObject captionObject = captionsArr.getJSONObject(i);
                        stringBuilder.append((String) captionObject.get("text")).append("\n");
                        Toast toast = Toast.makeText(MainActivity.this, "SUCCESS", Toast.LENGTH_SHORT);
                        toast.show();
                        tv.setText(stringBuilder.toString());
                    }
                } catch (JSONException e) {
                    Toast toast = Toast.makeText(MainActivity.this, "FAIL1", Toast.LENGTH_SHORT);
                    toast.show();
                    e.printStackTrace();
                }
            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast toast = Toast.makeText(MainActivity.this, "FAIL2", Toast.LENGTH_SHORT);
                toast.show();
                tv.setText(error.getMessage());
            }
        })
   {
       @Override
       public Map<String, String> getHeaders() throws AuthFailureError {
           HashMap headers = new HashMap();
           headers.put("Content-Type", "application/octet-stream");
           headers.put("Ocp-Apim-Subscription-Key", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
           return headers;
       }

       @Override
       public byte[] getBody() {
           Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test);
           ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
           bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
           byte[] imageData = byteArrayOutputStream.toByteArray();
           return imageData;
       }
   };

   RequestQueue requestQueue = Volley.newRequestQueue(this);
   requestQueue.add(jsonObjectRequest);
}

互联网上关于使用application/octet-stream进行Volley POST请求的资源很少,所以我在这里有点迷路了……

任何帮助将不胜感激!

0 个答案:

没有答案
相关问题