-1

我一直按照此处列出的步骤通过HttpURLConnection. 就我而言,有一个名为“json”的 POST 参数需要从服务器上拉出_POST['json']

问题是_POST['json'],除了我尝试上传文件时, 中没有任何内容;文件数据放置在 中_POST['json'],而不是_FILES['filename'].

目标是实现这一目标:

_POST['json'] = {"fileData":"Some extra data about the file(s)"}
_FILES['filename'] = the file I uploaded.

我构建请求的代码是:

try {
            url  = new URL(_requestObject.getUrl());
            conn = (HttpURLConnection)url.openConnection();

            conn.setRequestProperty("Content-Type", MMWebAPIConstants.MM_REQUEST_CONTENT_TYPE_HF + MMWebAPIConstants.MM_REQUEST_BOUNDARY);
            conn.addRequestProperty("Connection", "Keep-Alive");
            conn.setRequestMethod("POST");
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setUseCaches(false);

            if ( ! _requestObject.getAction().equals(MMWebAPIConstants.ACTION_LOGIN) ) {
                for ( String cookie : MMSessionManager.getSharedInstance().getCookies() )
                    conn.addRequestProperty("Cookie", cookie);
            }

            outstream = conn.getOutputStream();
            writer    = new PrintWriter(new OutputStreamWriter(outstream, "UTF-8"), true);

            writer.append("--" + MMWebAPIConstants.MM_REQUEST_BOUNDARY).append(crlf);
            writer.append("Content-Disposition: form-data; name=\"json\"").append(crlf);
            writer.append("Content-Type: text/plain; charset=UTF-8").append(crlf);
            writer.append(postValue.toString()).append(crlf).flush();

            if ( hasfiles ) {
                byte[]      bytes = null;
                InputStream input = null;

                for ( String filename : _requestObject.getFilePaths() ) {
                    String filepath = this.getContext().getFilesDir().getAbsolutePath() + "/" + filename;
                    String type     = URLConnection.guessContentTypeFromName(filename);
                    bytes           = new byte[5120];
                    input           = new FileInputStream(filepath);

                    writer.append("--" + MMWebAPIConstants.MM_REQUEST_BOUNDARY).append(crlf);
                    writer.append("Content-Disposition: form-data; name=\"" + filename + "\"; filename=\"" + filename + "\"").append(crlf);
                    writer.append("Content-Type: " + type).append(crlf);
                    writer.append("Content-Transfer-Encoding: binary").append(crlf);
                    writer.append(crlf).flush();

                    for ( int l = 0; (l = input.read(bytes)) > 0; ) {
                        outstream.write(bytes, 0, l);
                    }

                    outstream.flush();
                    writer.append(crlf).flush();
                }
            }

            writer.append("--" + MMWebAPIConstants.MM_REQUEST_BOUNDARY + "--").append(crlf);

            responseObject = this.getResponse(conn);

            this.getCookiesFromRequest(conn);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if ( writer != null ) writer.close();
            if ( conn != null ) conn.disconnect();
        }

post 和 files 数组如下所示:

ERROR - 2012-08-02 18:49:59 --> POST: Array
(
    [json] => PNG


IHDR#JzsBIT|d
4IDATx͒7rcd).`Tv-IHMH @55k.c_@qّ)f3Zz*UK%귿6}OGI~IJ~:M|@eӼXw|0`?b        Fլq^$zAn.;Cضo=
9J2O; e:=Md?DI{a;Lr56xA+~ҍZO|{orB{Iu<VkWL<x<m=+ȡhE5$:ͲI\k `9_5Ofx7EX"D#'NO:ƧjS+uj:gjP=,rɳW3I    s>DM4ޖ)մ_*``@JZ6(ʝZ3\ u5삮*rת@Rkc$o=JRs$%aϮi]ځl;]Q7lI6K  \ `L?OAsشZQOٍߍ*Y7VyoZuJYq,iM`SyYUSJfjѮf["0%y5N_0Z5"\ߚ$/fC!-ca^Mpfizvߚ???,)ak"_bUE>ml,BcMSB[7y')O7I7c>m3;Or6]qrAfe8t]I>0Zc!6Ϡ8
YT0\YA\?<#'GXGI^c"<Ͳ]cU;k$'y/z$Ƣ&N g<-0Џr'IRJAŗ9|ʉj3IL[Yo<fugq2;-acY[cw/Ô#,U /X"0x,k>uF&LRN$snu;דO    ~M</aSQ[Sy˶<*z`(RByP4Wh `L1=X   s3ٖ'9VaJ WML
0&|k:MB|LRNĚU<S|z`AWt7010&)'v/z%{cn=6lU9dW]"۔Ϫ<LFH_cNoy.0&SNT"fR.{0* `l<RKr<Ͳ?O;ÔxyػI~lU;=Jr@]  yMa&y=W}cbrƬs)aO_!<`삣$ea.  jUC~ơ+=6Zu-!8HfJ.B `@MRNDW }\wlҽ$$ߏ=`@]%ɿ9 Uɓq͗g)!Ъ*b!0H `4q|=LVO7>#MW*yp.   A͔m_/srr|We!]}  7GI˫OojRBK>gDK~@|܀W????[v9M8K9V4.Gtq܅'9+zؼU!U5z8:)Mr'9dy~ZB5IxE40  (Ƀ&NRNk(46+ܩU;}5ZnGI(EpA;I.n2Cۣ'`4pj5A]
 ס
&ݱwb<gxGY .nqS*͞0СiسjV4uUߴvB2
!NV~r[;i`@5M    n,k:٬خI4ZԾ6uIt1.Xs  [Jg8ZW$tqQ)ByM3i/moJ@1mo7XnFi=&ͪWBU}Sp%i%xjMIENDB`
)

ERROR - 2012-08-02 18:49:59 --> FILES: Array
(
)

更新

作为试验,我尝试关注这个线程(它引发了运行时异常),并且我尝试不上传文件,它不会填充_POST['json'],但仍然引发运行时异常。

请在这方面寻求帮助。

4

1 回答 1

-2

看起来您没有正确添加帖子参数。您需要发布参数。

请参阅:在 Android 中使用带有 post 参数的 HttpClient 和 HttpPost

于 2012-08-02T18:49:09.820 回答