apache httpcomponents.httpClient奇怪的错误

时间:2014-06-08 12:52:15

标签: java android apache httpclient

我们正在开发一个Android应用程序,我目前的部分是实现上传文件。 我使用的是httpcore-4.2.2,httpclient-4.2.2和httpmime-4.2.2。

我现在的问题是,如果URI具有非常具体的描述,该文件将仅与httpclient一起发送...如果我使用另一个URI,则服务器端的post-body是空的...我是真的无法理解这个问题。 我的代码如下:

public static String getUploadLink()  {

    String server = Mobile4dApplication.getContext().getString(R.string.default_server_address);
    String parameter = "/Files/upload";


    String finalQuery = String.format("%s%s", server, parameter);
    Log.d("UploadService", finalQuery );
    return finalQuery;
}

private String uploadFile(String uploadURL, File uploadFile, String title, long id) throws UnsupportedEncodingException, FileNotFoundException {
        MultipartEntity multipartEntity = new MultipartEntity(  );
        multipartEntity.addPart( "file", new FileBody( uploadFile ) );
        HttpResult httpResult;
        try {
            httpResult = HttpCommunicator.sendPOST( uploadURL, multipartEntity );
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        // TODO remove sysout line when it is not needed anymore
        System.out.println("result: " + httpResult.getResultMessage());
        return httpResult.getResultMessage();
    }

public static HttpResult sendPOST(String targetURL, HttpEntity entity) throws IOException {
    HttpClient httpClient = getHttpClient();
    HttpParams httpParams = httpClient.getParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, connectionTimeoutMillis);
    HttpConnectionParams.setSoTimeout(httpParams, socketTimeoutMillis);
    HttpPost httppost = new HttpPost(targetURL);
    httppost.setEntity(entity);
    HttpResponse response = httpClient.execute(httppost);
    String result = "";
    if (response != null && response.getEntity() != null) {
        result = EntityUtils.toString(response.getEntity(), UTF_8_ENCODING);
    }

    return new HttpResult(result, response.getStatusLine().getStatusCode());
}

如果我使用URI serveraddress / Files / upload,一切在服务器端看起来很好。但如果我用"文件/上传"更改部分甚至在最轻微的时候,当它到达我们的服务器时,整个后体变得空洞...... 我完全没有想法......我真的希望有人可以提供帮助......

0 个答案:

没有答案
相关问题