将音频(字节数组)发布到 MVC.NET Web Api

Cha*_*let 2 c# audio asp.net-mvc android asp.net-web-api

问题

我正在尝试将 Android 设备录制的音频发送到 MVC.NET Web Api。我通过传递简单的字符串参数来确认连接。然而,当我尝试传递从音频生成的字节数组时,我每次都会从服务器得到 500。我尝试了多种配置,但这是我目前拥有的:

MVC 网络 API

public class PostParms
{
    public string AudioSource { get; set; }
    public string ExtraInfo { get; set; }
}

public class MediaController : ApiController
{
    [HttpPost]
    public string Post([FromBody]PostParms parms)
    {
        byte[] bytes = System.Text.Encoding.UTF8.GetBytes(parms.AudioSource);
        return "success";
    }
}
Run Code Online (Sandbox Code Playgroud)

安卓代码

public class WebServiceTask extends AsyncTask<String, Void, Long>
{   
    @Override
    protected Long doInBackground(String... parms)
    {
        long totalsize = 0;
        String filepath = parms[0];
        byte[] fileByte = convertAudioFileToByte(new File(filepath));        

        //Tried base64 below with Convert.FromBase64 on the C# side
        //String bytesstring = Base64.encodeToString(fileByte, Base64.DEFAULT);

        String bytesstring = "";
        try 
        {
            String t = new String(fileByte,"UTF-8");
    bytesstring = t;
        } catch (UnsupportedEncodingException e1)
        {
        // TODO Auto-generated catch block
        e1.printStackTrace();
        }

        URL url;
        HttpURLConnection urlConnection = null;
        try {
            url = new URL("http://my.webservice.com:8185/api/media");

            //setup for connection
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setUseCaches(false);
            urlConnection.setRequestMethod("POST");
            urlConnection.setDoOutput(true);
            urlConnection.setRequestProperty("Content-Type","application/json");
            urlConnection.setRequestProperty("Accept","application/json");
            urlConnection.connect();

            JSONObject json = new JSONObject();
            json.put("AudioSource", bytesstring);
            json.put("ExtraInfo", "none");

            DataOutputStream output = new DataOutputStream(urlConnection.getOutputStream());
            output.writeBytes(URLEncoder.encode(json.toString(),"UTF-8"));
            output.flush();
            output.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally {
            urlConnection.disconnect();
        }
        return totalsize;
    }

    protected void onPreExecute(){

    }
    protected void onPostExecute(){

    }
    private byte[] convertAudioFileToByte(File file) {
        byte[] bFile = new byte[(int) file.length()];

        try {
            // convert file into array of bytes
            FileInputStream fileInputStream = new FileInputStream(file);
            fileInputStream.read(bFile);
            fileInputStream.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return bFile;
    }
}
Run Code Online (Sandbox Code Playgroud)

我可以让 .NET 应用程序使用相同的 API 发送音频流,但不能使用 Android。就像我说的,我已经尝试了在互联网上找到的许多配置,包括编码和输出流,但仍然得到 500。我需要帮助,因为我不知所措。

提前致谢

小智 5

我用了 aByteArrayEntity而不是 a StringEntity

// ByteArrayEntity 

HttpPost request = new HttpPost(getString(R.string.server_url) + "SendMessage");
ByteArrayEntity be = new ByteArrayEntity(msg);
be.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded"));
request.setEntity(be);
HttpResponse response = client.execute(request);
Run Code Online (Sandbox Code Playgroud)

在服务器端,我使用以下 Web API 代码来获取字节数组:

[HttpPost]
public async Task<string> SendMessage()
{
    byte[] arrBytes = await Request.Content.ReadAsByteArrayAsync();
    return "";
}
Run Code Online (Sandbox Code Playgroud)