1

コンテンツの長さが必要な411が常に返されますが、ここで何が間違っているのでしょうか。私はそれを正しく送っていませんか?RoRサーバーに送信しています。基本的には、画像の有無に応じてステートメントを切り替えます。みんなありがとう。

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

1 に答える 1

2

コンテンツの長さは、http リクエスト プロパティではなく、Http リクエスト ヘッダー プロパティです。

これは、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()を使用して、コンテンツの長さを設定してみてください。次に例を示します。

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

お役に立てれば。

于 2012-04-04T23:42:07.163 に答える