2016-03-29 6 views
0

写真をアップロードしてJSON文字列をサーバーに渡そうとしています。HTTPURLConnectionバッファエラー

私はコンテンツの長さを間違って計算しています。しかし、私の数学の問題がどこにあるのか分かりません。

誰でも私のエラーを追跡するのに役立つことができますか?

E/PhotoUpload: error: expected 14591 bytes but received 15183 java.net.ProtocolException: expected 14591 bytes but received 15183 at com.android.okhttp.internal.http.HttpConnection$FixedLengthSink.write(HttpConnection.java:311) at com.android.okio.RealBufferedSink.flush(RealBufferedSink.java:154) at com.android.okio.RealBufferedSink$1.flush(RealBufferedSink.java:137) at java.io.FilterOutputStream.flush(FilterOutputStream.java:88) at java.io.DataOutputStream.flush(DataOutputStream.java:63) at com.mycompany.myapp.TakePhotoActivity$BGUploadImage.doInBackground(TakePhotoActivity.java:381) at com.mycompany.myapp.TakePhotoActivity$BGUploadImage.doInBackground(TakePhotoActivity.java:273)

E/PhotoUpload: error: expected 14591 bytes but received 15183 java.net.ProtocolException: expected 14591 bytes but received 15183 at com.android.okhttp.internal.http.HttpConnection$FixedLengthSink.write(HttpConnection.java:311) at com.android.okio.RealBufferedSink.flush(RealBufferedSink.java:154) at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:769) at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:405) at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:349) at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:203) at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210) at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:25) at com.mycompany.myapp.TakePhotoActivity$BGUploadImage.doInBackground(TakePhotoActivity.java:393) at com.mycompany.myapp.TakePhotoActivity$BGUploadImage.doInBackground(TakePhotoActivity.java:273) at android.os.AsyncTask$2.call(AsyncTask.java:292)

あなたがすべてで、コンテンツの長さを計算する、またはそれを送信していないと
public class BGUploadImage extends AsyncTask<String, Integer, Integer> { 
     int maxBufferSize = 1 * 256 * 1024; 
     int headerSize = 89; 
     String urlString = "http://www.mywebsite.com/someapi"; 
     int fileSize = 0; 
     int infoSize = 0; 
     private int i; 


     @Override 
     protected void onPreExecute() { 
     } 

     @Override 
     protected void onProgressUpdate(Integer... values) { 
     } 

     @Override 
     protected Integer doInBackground(String... file) { 

      Integer ret = 0; 

      HttpURLConnection conn = null; 
      DataOutputStream dos = null; 
      DataInputStream inStream = null; 

      String exsistingFileName = file[0]; 

      String lineEnd = "\r\n"; 
      String twoHyphens = "--"; 
      String boundary = "---------------------------14737809831466499882746641449"; 

      String json = JSONCalls.uploadImageJSON(thisCallKey); 

      int bytesRead, bytesAvailable, bufferSize; 
      byte[] buffer; 

      try { 
       Log.i("PhotoUpload", "Start uploading file: " + file[0]); 

       FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName)); 

       URL url = new URL(urlString); 

       conn = (HttpURLConnection) url.openConnection(); 
       conn.setDoInput(true); 
       conn.setDoOutput(true); 
       conn.setUseCaches(false); 

       fileSize = fileInputStream.available(); 
       infoSize = fileSize + headerSize + file[0].length(); 
       conn.setFixedLengthStreamingMode(infoSize); 
       conn.setRequestMethod("POST"); 
       conn.setRequestProperty("Connection", "Keep-Alive"); 
       conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); 

       dos = new DataOutputStream(conn.getOutputStream()); 

       //JSON PART 
       dos.writeBytes(twoHyphens + boundary + lineEnd); 
       dos.writeBytes("Content-Disposition: form-data; name='params'" + lineEnd); 
       dos.writeBytes(json); 
       dos.writeBytes(lineEnd); 

       //MEDIA PART 
       dos.writeBytes(twoHyphens + boundary + lineEnd); 
       dos.writeBytes("Content-Disposition: form-data; name = 'UploadedFile'; filename = '" + file[0] + "' " + lineEnd); 
       dos.writeBytes(lineEnd); 

       // create a buffer of maximum size 
       bytesAvailable = fileInputStream.available(); 
       bufferSize = Math.min(bytesAvailable, maxBufferSize); 
       buffer = new byte[bufferSize]; 

       // read file and write it into form... 
       bytesRead = fileInputStream.read(buffer, 0, bufferSize); 

       int bytesSent = 0; 
       while (bytesRead > 0) { 
        if (bytesSent > 0) { 
         int pg = (bytesSent * 100)/infoSize; 
         publishProgress(pg); 
        } 
        bytesSent += bufferSize; 
        dos.write(buffer, 0, bufferSize); 
        bytesAvailable = fileInputStream.available(); 
        bufferSize = Math.min(bytesAvailable, maxBufferSize); 
        bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
       } 

       // send multipart form data necesssary after file data... 
       dos.writeBytes(lineEnd); 
       dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 

       //close streams 
       fileInputStream.close(); 
       dos.flush(); 
       dos.close(); 

      } catch (MalformedURLException ex) { 
       Log.e("PhotoUpload", "error: " + ex.getMessage(), ex); 
       ret = 1; 
      } catch (IOException ioe) { 
       Log.e("PhotoUpload", "error: " + ioe.getMessage(), ioe); 
       ret = 1; 
      } 

      try { 
       inStream = new DataInputStream(conn.getInputStream()); 
       String str; 

       while ((str = inStream.readLine()) != null) { 
        Log.i("PhotoUpload", "Server Response" + str); 
       } 
       inStream.close(); 
      } catch (IOException ioex) { 
       Log.e("PhotoUpload", "error: " + ioex.getMessage(), ioex); 
       ret = 1; 
      } 

      return ret; 
     } 

     @Override 
     protected void onPostExecute(Integer r) { 

     } 

    } 
} 
+0

コンテンツの長さはどこにもありません。あなたはそれを設定していません。しかし、あなたはそれをする必要はありません。 – greenapps

+0

あなたのログから: 'TakePhotoActivity.java:381'。その行にはどのコードがありますか? – greenapps

+0

'available()'も誤って使用しています。 Javadocを参照してください。あなたがここで何をしているのかを正確に警告します。 – EJP

答えて

0

My guess is I am incorrectly calculating the content-length.

、どのようなこの推測はできる理解することは困難である:

私は、次のエラーが発生しますおそらくベースになるでしょう。

あなたのコードには他にも問題があります。あなたのコピーループ:

// read file and write it into form... 
bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
int bytesSent = 0; 
while (bytesRead > 0) { 
    if (bytesSent > 0) { 
     int pg = (bytesSent * 100)/infoSize; 
     publishProgress(pg); 
    } 
    bytesSent += bufferSize; 
    dos.write(buffer, 0, bufferSize); 
    bytesAvailable = fileInputStream.available(); 
    bufferSize = Math.min(bytesAvailable, maxBufferSize); 
    bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
} 

は神聖な混乱です。 Javaコピーループはこれ以上複雑ではありません。

byte[] buffer = new byte[8192]; 
int count; 
while ((count = in.read(buffer)) > 0) 
{ 
    out,write(buffer, 0, count); 
} 

ファイルと同じサイズのバッファは必要ありません。これは単にスペースを無駄にしているだけです。また、ファイルサイズの尺度としてavailable()を使用するか、または十分な大きさのバッファを割り当てるために、特にJavadocで警告されます。それはそれが何のためではありません。 available()の正しい使用法はほとんどありません。これはその1つではありません。

flush()close()が冗長です。