2012-05-10 12 views
0

Android 3.0まで次のコードを使用してサーバーにファイルをアップロードできます: ICSの場合、httpurlconnection.getinputstreamを使用するとfilenotfoundeceptionが返されます 誰かがHTTPPOSTを使用してこのコードを変換することを提案しますが、どのように行うか分からない。ICSでAndroidファイルをアップロード

いくつかの提案はありますか?

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(); 

    } 
} 
+0

。私はまた、問題のコードを修正し、おそらく無関係な、しかし、』コンテンツ長」は』コンテンツな長さsetRequestProperty()。 – kcoppock

+0

はい、あなたは正しいです – Giuseppe

答えて

0

マイコンテンツ-な長さは下、コンテンツ長 『『あるべき』『

関連する問題