2012-04-04 2 views
1

Я всегда возвращаюсь Требуется длина контента 411, что я делаю неправильно здесь? я не отправляю его правильно? Я отправляю на сервер RoR. Это в основном будет переключаться между заявлениями в зависимости от погоды есть изображение или нет. Спасибо, парни.Что не так с этой многочастной формой? Android-Java

public static void executeMultipartPost(Bitmap image, String topicid, String topost, Context c) throws Exception { 
      String boundary = "------WebKitFormBoundary4QuqLuM1cE5lMwCy"; 

      ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
      image.compress(CompressFormat.JPEG, 45, bos); 
      byte[] data = bos.toByteArray(); 
      try 
      { 

       URL post_url = new URL("https://myapi"); 
       HttpURLConnection feed_connection = (HttpURLConnection) post_url.openConnection(); 
       feed_connection.setChunkedStreamingMode(0); 
       feed_connection.setDoOutput(true);//make a POST Method as defualt is GET 
       feed_connection.setRequestMethod("POST"); 
       feed_connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); 


       //Now the actual composing of the multi part form 
       String session_key = new String("--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"session_key\"\r\n\r\n" + key + "\r\n"); 
       String feed = new String("--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"feed\"\r\n\r\n" + topost + "\r\n"); 
       String sh_career = new String("--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"share_to_career_team\"\r\n\r\n" + 0 + "\r\n"); 
       String sh_friend = new String("--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"share_to_friend\"\r\n\r\n" + 0 + "\r\n"); 
       String url_t = new String("--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"url_title\"\r\n\r\n"+"\r\n"); 
       String url_d = new String("--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"url_description\"\r\n\r\n"+"\r\n"); 
       String url_a = new String("--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"url_address\"\r\n\r\n"+"\r\n"); 
       String topic_id = new String("--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"post_to\"\r\n\r\n" + topicid + "\r\n"); 


       if (data != null) { 
        String file_param_constant = "image"; 
        Log.d("TAG","Image is here"); 
        String image_part_1 = new String("--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"richmedia_type\"\r\n\r\n" + "1\r\n"); 
        String image_part_2 = new String("--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"" + file_param_constant + "\"; filename=\"image.jpg\"\r\n" + "Content-Type: image/jpeg\r\n\r\n"); 
        String missing_link = new String ("\r\n"); 
        String image_part_3 = new String("--" + boundary + "--\r\n"); 
        feed_connection.setRequestProperty("Content-Length", String.valueOf((session_key.length() + feed.length() + sh_career.length() + sh_friend.length() + url_t.length() + url_d.length() + url_a.length() + topic_id.length() + image_part_1.length() + image_part_2.length() + missing_link.length() + image_part_3.length()))); 
        Log.d("TAG", "CONTENT LEGNTH WITH IMG:"+String.valueOf((session_key.length() + feed.length() + sh_career.length() + sh_friend.length() + url_t.length() + url_d.length() + url_a.length() + image.getByteCount() + topic_id.length()))); 
        ByteArrayOutputStream form_output = new ByteArrayOutputStream(); 
        OutputStream form_stream   = new BufferedOutputStream(feed_connection.getOutputStream()); 
        form_output.write(session_key.getBytes()); 
        form_output.write(feed.getBytes()); 
        form_output.write(sh_career.getBytes()); 
        form_output.write(sh_friend.getBytes()); 
        form_output.write(url_t.getBytes()); 
        form_output.write(url_d.getBytes()); 
        form_output.write(url_a.getBytes()); 
        form_output.write(topic_id.getBytes()); 
        form_output.write(image_part_1.getBytes()); 
        form_output.write(image_part_2.getBytes()); 
        form_output.write(data); 
        form_output.write(missing_link.getBytes()); 

        form_output.write(image_part_3.getBytes()); 

        form_stream.write(form_output.toByteArray()); 
        Log.d("TAG","Feed-Connection content legnth :"+Integer.toString(feed_connection.getContentLength())); 

       }else { 
        String image_part_1 = new String("--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"richmedia_type\"\r\n\r\n" + "0\r\n"); 
        Log.d("TAG", "CONTENT LEGNTH:"+String.valueOf((session_key.length() + feed.length() + sh_career.length() + sh_friend.length() + url_t.length() + url_d.length() + url_a.length() + image.getByteCount() + topic_id.length()))); 
        feed_connection.setRequestProperty("Content-Length NO IMG", String.valueOf((session_key.length() + feed.length() + sh_career.length() + sh_friend.length() + url_t.length() + url_d.length() + url_a.length() + image.getByteCount() + topic_id.length()))); 
        ByteArrayOutputStream form_output = new ByteArrayOutputStream(); 
        OutputStream form_stream   = new BufferedOutputStream(feed_connection.getOutputStream()); 
        form_output.write(session_key.getBytes()); 
        form_output.write(feed.getBytes()); 
        form_output.write(sh_career.getBytes()); 
        form_output.write(sh_friend.getBytes()); 
        form_output.write(url_t.getBytes()); 
        form_output.write(url_d.getBytes()); 
        form_output.write(url_a.getBytes()); 
        form_output.write(topic_id.getBytes()); 
        form_output.write(image_part_1.getBytes()); 
        form_stream.write(form_output.toByteArray()); 
        Log.d("TAG","Feed-Connection content legnth :"+ Integer.toString(feed_connection.getContentLength())); 
       } 
       //write form to connection 

       if (feed_connection.getResponseCode() != HttpURLConnection.HTTP_OK) 
       { 
        Log.d("TAG","CRAP BROKE MAN"); 
        throw new Exception("Carp hit the fan an connection didnt get a OKAY Error Code: " + feed_connection.getResponseMessage() + feed_connection.getResponseCode()); 
       } 

       InputStream recieved_information = feed_connection.getInputStream(); 


      } 
      catch (Exception the_e) 
      { 
       System.out.println("Not even" + the_e.getMessage()); 
       Log.d("TAG", "NOT EVEN" + the_e.getMessage()); 
      } 
     } 

ответ

2

Длина содержимого - это свойство заголовка запроса Http, а не свойство запроса HTTP.

Это не устанавливает Content Length в заголовке запроса HTTP:

feed_connection.setRequestProperty("Content-Length", String.valueOf((session_key.length() + feed.length() + sh_career.length() + sh_friend.length() + url_t.length() + url_d.length() + url_a.length() + topic_id.length() + image_part_1.length() + image_part_2.length() + missing_link.length() + image_part_3.length()))); 

Попробуйте использовать HttpURLConnection.setFixedLengthStreamingMode() установить Content Length, например:

feed_connection.setFixedLengthStreamingMode(session_key.length() + feed.length() + sh_career.length() + sh_friend.length() + url_t.length() + url_d.length() + url_a.length() + topic_id.length() + image_part_1.length() + image_part_2.length() + missing_link.length() + image_part_3.length()); 

Надеется, что это помогает.

+0

попробуйте сейчас спасибо! – user824015

+0

Кажется, что-то сделано, но теперь я получаю сообщение об ошибке «Уже в режиме chunked» – user824015

+0

поэтому я прокомментировал feed_connection.setChunkedStreamingMode (0); и теперь я ожидаю 1139 байт, но получил 9714, так близко! – user824015

Смежные вопросы