【问题标题】:Send XML to PHP server with Android使用 Android 将 XML 发送到 PHP 服务器
【发布时间】:2013-09-27 09:01:44
【问题描述】:

我尝试了很多不同的解决方案来将 XML 文件发布到 PHP 服务器。

响应总是“09-27 10:49:10.550: I/TAG(3950): File MIME-TYPE was not recognized.”

最后版本的代码是:

    httppost.setHeader("Content-Type","text/xml;charset=\"UTF-8\"");

    String textToUpload = "";
    try {
        textToUpload = getStringFromFile(fileName);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return new StringEntity(textToUpload);

在初始化连接之前是这样的:

private void openConnection () throws IOException {
    httpclient = new DefaultHttpClient();
    httppost = new HttpPost(url);
}

并且在这样处理响应之后:

private int getServerResponse() throws IOException {
    HttpResponse response = httpclient.execute(httppost);
    Log.i("CIAO", EntityUtils.toString(response.getEntity()));
    StatusLine statusLine = response.getStatusLine();
    return statusLine.getStatusCode();
}

我也尝试过这个解决方案(以及许多不同的解决方案),但没有任何成功:

    File file = new File(fileName);
    httppost.setHeader("Content-Type","text/xml;charset=UTF-8");

    ContentBody fb = new FileBody(file, "text/xml");
    MultipartEntity entity = new MultipartEntity(
            HttpMultipartMode.STRICT);
    entity.addPart("file", fb);
    httppost.setEntity(entity);

有什么想法吗?

【问题讨论】:

    标签: php android xml http-post multipart


    【解决方案1】:

    我是这样解决问题的:

        File file = new File(fileName);
    
        ContentBody fb = new FileBody(file, "text/xml");
        MultipartEntity entity = new MultipartEntity(
                HttpMultipartMode.BROWSER_COMPATIBLE);
        entity.addPart("file", fb);
        httppost.setEntity(entity);
        return entity;
    

    我不知道问题是 STRICT 还是标题集.. 无论如何,现在它可以工作了.. :)

    【讨论】:

      猜你喜欢
      • 2012-06-13
      • 1970-01-01
      • 1970-01-01
      • 2014-02-24
      • 2012-07-19
      • 2011-02-02
      • 2018-02-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多