0

Android 3.0までは、次のコードを使用してサーバーにファイルをアップロードできます。ICSでは、httpurlconnection.getinputstreamを使用すると、コードはfilenotfoundexceptionを返します。誰かがHTTPPOSTを使用してこのコードを変換することを提案していますが、正直なところ、方法がわかりません。

いくつかの提案?

public class FileUpload extends AsyncTask<Void, Integer, Void> {

    @Override
    protected void onProgressUpdate(Integer... progress) {
        dialog.setProgress(progress[0]);
    }

    private ProgressDialog dialog;
    private String reponse_data;
    private String file_path;
    private JobInstance localJob;
    private Context localContext;
    private CharSequence testo;
    private HttpURLConnection connection;

    // can use UI thread here
    protected void onPreExecute() {
        dialog = new ProgressDialog(localContext);
        dialog.setMessage("Uploading...");
        dialog.setIndeterminate(false);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setProgress(0);
        dialog.show();
    }

    @Override
    protected Void doInBackground(Void... arg0) {

        try {
            File file = new File(file_path);
            FileInputStream fileInputStream = new FileInputStream(file);
            byte[] bytes = new byte[(int) file.length()];
            fileInputStream.read(bytes);
            fileInputStream.close();

            URL url = new URL("https://myserver.com/functionUpload?lang=it-IT");
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("Content-Type", "audio/x-flac; rate=16000");
            connection.setRequestProperty("Content-Length", String.valueOf(bytes.length));

            if(Build.VERSION.SDK_INT < 11){
                connection.setDoOutput(true);
            }



            //connection.connect();     
            OutputStream outputStream = connection.getOutputStream();

            int bufferLength = 1024;
            for (int i = 0; i < bytes.length; i += bufferLength) {
                int progress = (int) ((i / (float) bytes.length) * 100);
                publishProgress(progress);
                if (bytes.length - i >= bufferLength) {
                    outputStream.write(bytes, i, bufferLength);
                } else {
                    outputStream.write(bytes, i, bytes.length - i);
                }
                outputStream.flush();
            }

            publishProgress(100);

            outputStream.close();


        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            int risp = connection.getResponseCode();
                DataInputStream inStream = new DataInputStream ( connection.getInputStream() );
            String str;
            while (( str = inStream.readLine()) != null){
                Log.e("Debug","Server Response "+str);
                reponse_data=str;

                    JSONObject jObject = new JSONObject(reponse_data);
                    JSONArray menuObject = jObject.getJSONArray("hypotheses");
                    testo = menuObject.getJSONObject(0).getString("utterance").toString();
            }
            inStream.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;




    }

    @Override
    protected void onPostExecute(Void result) {
        if (this.dialog.isShowing()) {
            this.dialog.dismiss();
        }
        Toast.makeText(localContext, testo, Toast.LENGTH_LONG).show();
    }

    public void executeBefore(Context context, JobInstance j) {
        localContext = context;
        dialog = new ProgressDialog(localContext);
        localJob = j;
        this.file_path = localJob.getFinalfileposition().getAbsolutePath();
        this.execute();

    }
}
4

1 に答える 1

0

私のContent-Lenght」は「Content-Length」である必要があります。質問のコードも修正します。

于 2012-07-06T05:57:42.960 に答える