1

晚上好。

我有这样的发帖请求

-----------------------------17911109517875 Content-Disposition: form-data;
name="PERSON*1[F*2][2664]" value1 
-----------------------------17911109517875 Content-Disposition: form-data; 
name="PERSON*1[I*3][2776]" value2 
-----------------------------17911109517875 Content-Disposition: form-data;  
name="PERSON*1[O*4][2778]" value3

并尝试通过 HttpClient 从 android 设备发送它。

public String doMultipartPost(String url, List<NameValuePair> urlParameters) {

    HttpContext localContext = new BasicHttpContext();
    localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

    HttpPost request = new HttpPost(url);
    HttpProtocolParams.setUserAgent(client.getParams(), "My funcy UA");

    MultipartEntity entity = new 
            MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

    for (NameValuePair nvp : urlParameters) {
        try {
            entity.addPart(nvp.getName(), new StringBody(nvp.getValue()));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    request.setEntity(entity);

    try {
        HttpResponse response = client.execute(request, localContext);
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return "post";

}

但是-----------------------------17911109517875 Content-Disposition: form-data; 它是由 MultipartEntity 自动设置的,还是我应该在代码中的某处写呢?此代码不起作用。

谢谢。

4

1 回答 1

0

我在个人项目中有类似的代码。这个 SO question也可以给你一些指导。

边界由库自动设置,因此您不必照顾它们,具体取决于 HttpMultipartMode 的语法。我使用了默认的 STRICT 模式。

您是否添加了所需的其他库?那些对我有用...

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.2.3</version>
</dependency>
<dependency>
    <groupId>org.apache.james</groupId>
    <artifactId>apache-mime4j-core</artifactId>
    <version>0.7.2</version>
</dependency>

您能否提供有关错误的更多信息:logcat 中的跟踪或服务器上的日志?

于 2013-07-09T09:17:09.023 回答