2016-11-29 8 views
0

私はいくつかのデータをHeroku/PHPサーバにアップロードしようとしていますが、何らかの理由でそれが一貫して動作していません。それは私のオフィスで私にとって完璧に動作しますが、他の人が私たちのアプリを使用しようとすると、ファイルはサーバーに届きません。サーバーが「空のファイル」エラーを返します。ここでAndroidファイルのアップロード作業が一貫して行われない

は、Androidからのアップロードのコードです:

private class SubmitClientTask extends AsyncTask<URL, Integer, Long> { 
    AlertDialog.Builder mDialog; 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     mDialog = new AlertDialog.Builder(ClientSignActivity.this); 
     mDialog.setCancelable(false); 
    } 

    protected Long doInBackground(URL... urls) { 
     //int count = urls.length; 
      long totalSize = 0; 
      String response = ""; 
      File signatureFile = new File(signaturePhotoPath); 
      File photoFile = new File(StaticValues.currentPhotoPath); 
      String boundary = "*****";//Long.toHexString(System.currentTimeMillis()); 
      String CRLF = "\r\n"; 
      String charset = "UTF-8"; 
      try { 
       URLConnection mConnection = urls[0].openConnection(); 
       mConnection.setDoOutput(true); 
       ((HttpURLConnection)mConnection).setRequestMethod("POST"); 
       //mConnection.setRequestProperty("Connection", "Keep-Alive"); // would this line of code help? 
       mConnection.setRequestProperty("Cache-Control", "no-cache"); // this one? 
       mConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); 

       OutputStream output = mConnection.getOutputStream(); 
       PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset)); 

       writer.append("--" + boundary).append(CRLF); 
       writer.append("Content-Disposition: form-data; name=\"dentistName\"").append(CRLF); 
       writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); 
       writer.append(CRLF).append("A string here").append(CRLF).flush(); 

       writer.append("--" + boundary).append(CRLF); 
       writer.append("Content-Disposition: form-data; name=\"patientName\"").append(CRLF); 
       writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); 
       writer.append(CRLF).append("Another string here").append(CRLF).flush(); 

       writer.append("--" + boundary).append(CRLF); 
       writer.append("Content-Disposition: form-data; name=\"patientPhone\"").append(CRLF); 
       writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); 
       writer.append(CRLF).append("A third string").append(CRLF).flush(); 

       //set up image stuff 
       InputStream is; 
       int c; 
       byte[] buf; 

       //send image 
       writer.append("--" + boundary).append(CRLF); 
       writer.append("Content-Disposition: form-data; name=\"patientPhoto\"; filename=\"" + photoFile.getName() + "\"").append(CRLF); 
       writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); 
       writer.append(CRLF).flush(); 
       c = 0; 
       is = new FileInputStream(StaticValues.patientBitmapToSend); 
       buf = new byte[8192]; 
       while ((c = is.read(buf, 0, buf.length)) > 0) { 
        output.write(buf, 0, c); 
        output.flush(); 
       } 
       output.flush(); 
       writer.append(CRLF).flush(); 

       //send another photo 
       writer.append("--" + boundary).append(CRLF); 
       writer.append("Content-Disposition: form-data; name=\"signaturePhoto\"; filename=\"" + signatureFile.getName() + "\"").append(CRLF); 
       writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); 
       writer.append(CRLF).flush(); 
       c = 0; 
       is = new FileInputStream(signatureFile); 
       buf = new byte[8192]; 
       while ((c = is.read(buf, 0, buf.length)) > 0) { 
        output.write(buf, 0, c); 
        output.flush(); 
       } 
       output.flush(); 
       writer.append(CRLF).flush(); 

       //end of multipart/form-data 
       writer.append("--" + boundary + "--").append(CRLF).flush(); 
       Log.d("content: ", mConnection.getContent().toString()); 
       int responseCode = ((HttpURLConnection) mConnection).getResponseCode(); 

       if (responseCode == HttpsURLConnection.HTTP_OK) { 
        String line; 
        BufferedReader br = new BufferedReader(new InputStreamReader(mConnection.getInputStream())); 
        while ((line = br.readLine()) != null) { 
         response += line; 
        } 
        sendStuffResponse = response; 
       } else { 
        response = "failed"; 

       } 

       //finally, 
       Log.d("Response: ", response); 

       ((HttpURLConnection) mConnection).disconnect(); 

       //cleanup 
       (new File(StaticValues.currentPhotoPath)).delete(); 
       photoFile.delete(); 
       signatureFile.delete(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      return totalSize; 
    } 

    protected void onProgressUpdate(Integer... progress) { 
     //setProgressPercent(progress[0]); 
    } 

    protected void onPostExecute(Long result) { 
     super.onPostExecute(result); 

     progressDialog.dismiss(); 

     mDialog.setTitle(R.string.upload_success_title); 
     mDialog.setMessage(R.string.upload_success_str); //DEBUG sendStuffResponse 
     mDialog.setPositiveButton(R.string.ok_str, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int id) { 
         finish(); 
        } 
       }); 
     mDialog.create().show(); 
     //Toast.makeText(getApplicationContext(), "Downloaded " + result + " bytes", Toast.LENGTH_LONG).show(); 
    } 
} 

任意の助けをいただければ幸いです。ありがとう!

+0

'キャッチ(例外e){ e.printStackTrace(); } '。そのキャッチブロックは空です。だからキャッチがある場合、それについてユーザーにどのように通知しますか?現時点ではまったくではありません。したがって、ユーザーは何かが間違っているかどうかを知ることはありません。そしてあなたも。 – greenapps

+0

良い点は、私はエラーメッセージを追加する必要がありますが、私は169バイトしか作成されておらず、ファイルが空であることがわかっているので、イメージがサーバーに到達しないことを知っています。また、応答コードは500です。 –

+0

サーバーは内部ネットワークからのみアクセスできますか?外部のネットワークとあなたのネットワークの間にファイアウォールはありますか? –

答えて

0

私は何が間違っているかを知りました。すべてのイメージデータが到着する前に、サーバー上のスクリプトが実行されていました。 Herokuの直接S3に画像を送信するように設定されている:

https://devcenter.heroku.com/articles/s3
関連する問題