2017-10-01 13 views
0

私はいくつかのマルチパートフォームデータをAndroid APIからサーバーAPIに送信しようとしています。ここで私は問題を解析に直面している私のコードです。私は3つの値を掲示しています。 2つはテキスト値で、1つはファイルです。 3つのうち、の埋め込みテキスト値は正しく解析されません。解析エラーのないJavaで複数部分のフォームデータを送信するには?

すべての値を正しく送信できるようにするには、コードで何を修正する必要がありますか?

try 
    { 
      Log.e(Tag); 

      // Open a HTTP connection to the URL 
      HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection(); 
      // conn.setConnectTimeout(); 
      // conn.setReadTimeout(); 
      // Allow Inputs 
      conn.setDoInput(true); 

      // Allow Outputs 
      conn.setDoOutput(true); 

      // Don't use a cached copy. 
      conn.setUseCaches(false); 

      // Use a post method. 
      conn.setRequestMethod("POST"); 

      conn.setRequestProperty("Connection", "Keep-Alive"); 

      conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); 

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

      // dos.writeBytes(twoHyphens + boundary + lineEnd); 
      dos.writeBytes("Content-Disposition: form-data; name=\"infill\""+ lineEnd); 
      dos.writeBytes(lineEnd); 
      dos.writeInt(infill_); 
      dos.writeBytes(lineEnd); 

     dos.writeBytes("Content-Disposition: form-data; name=\"material-type\""+ lineEnd); 
      dos.writeBytes(lineEnd); 
      dos.writeInt(materialType); 
      dos.writeBytes(lineEnd); 
      dos.writeBytes(twoHyphens + boundary + lineEnd); 

      dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + iFileName +"\"" + lineEnd); 
      dos.writeBytes(lineEnd); 

      // Log.e(Tag,"Headers are written"); 

      // create a buffer of maximum size 
      int bytesAvailable = fileInputStream.available(); 

      int maxBufferSize = 1024; 
      int bufferSize = Math.min(bytesAvailable, maxBufferSize); 
      byte[ ] buffer = new byte[bufferSize]; 

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

      while (bytesRead > 0) 
      { 
        dos.write(buffer, 0, bufferSize); 
        bytesAvailable = fileInputStream.available(); 
        bufferSize = Math.min(bytesAvailable,maxBufferSize); 
        bytesRead = fileInputStream.read(buffer, 0,bufferSize); 
      } 
      dos.writeBytes(lineEnd); 
      dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 

      // close streams 
      fileInputStream.close(); 

      dos.flush(); 

      Log.i("STL File Sent, Response code: "+String.valueOf(conn.getResponseCode())); 
      Log.i("STL File Sent, Response message : "+String.valueOf(conn.getResponseMessage())); 

      InputStream is = conn.getInputStream(); 

      // retrieve the response from server 
      int ch; 

      StringBuffer b =new StringBuffer(); 
      while((ch = is.read()) != -1){ b.append((char)ch); } 
      String response=b.toString(); 
      Log.i("STL Response + "+response); 
      dos.close(); 

      // Set cost and time 
      getDataFromJsonResponse(response);   
    } 
+0

解析エラーを取り除く最良の方法の1つは、Retrofitのようなライブラリを使用することです。 –

+0

解析の問題を詳しく説明してください。私たちに推測させてください。 – greenapps

+1

infiil整数は整数として送られると思います。したがって、テキストとして解析すると失敗します。整数もテキスト表現で投稿する必要があります。 – greenapps

答えて

0
dos.writeInt(infill_); 

これは、テキストではなくバイナリとして送信する必要があります。 HTMLはテキストプロトコルです。本当にDataOutputStreamを使用する理由はありません。

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

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

while (bytesRead > 0) 
{ 
    dos.write(buffer, 0, bufferSize); 
    bytesAvailable = fileInputStream.available(); 
    bufferSize = Math.min(bytesAvailable,maxBufferSize); 
    bytesRead = fileInputStream.read(buffer, 0,bufferSize); 
} 

これをすべて行う必要はありません。ジャスト:

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

は、もしあれば、正しい用法available()のいくつかありますが、これはその一つではありません。

関連する問題