Buchung Audio (byte-array) MVC.NET Web-Api

Das Problem

Ich versuche zu senden audio-aufgezeichnet von meinem android-Gerät auf einen MVC.NET Web-Api. Ich bestätigte den Zusammenhang durch die übergabe einfache string-Parameter. Jedoch, wenn ich versuche, pass auf das byte-array generiert aus den audio -, bekomme ich einen 500 server jedes mal. Ich habe versucht mehrere Konfigurationen, aber hier ist, was ich derzeit habe:

MVC-Web-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";
    }
}

Android-Code

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;
    }
}

Ich eine bekommen kann .NET-Anwendung zu arbeiten, mit der gleichen API sendet eine audio-stream, aber nicht Android. Wie ich schon sagte, ich habe versucht, eine Reihe von Konfigurationen im internet gefunden, die mit der Kodierung und der output-stream, aber weiterhin 500. Ich brauche Hilfe, da ich ratlos bin.

Vielen Dank im Voraus

InformationsquelleAutor Chase Caillet | 2014-02-12

Schreibe einen Kommentar