0

我正在尝试通过 MultipartEntity 上传图像和一些文本。我可以上传和接收图像,但是当我尝试添加 Stringbody 时,我似乎无法接收它。这是我的安卓代码

imports ETC...

public void oncreate(){
.....
nameValuePairs.add(new BasicNameValuePair("image", exsistingFileName));
nameValuePairs.add(new BasicNameValuePair("title", "title"));
}

public void post(String url, List<NameValuePair> nameValuePairs) {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(url);


try {
    MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

    for(int index=0; index < nameValuePairs.size(); index++) {
        if(nameValuePairs.get(index).getName().equalsIgnoreCase("image")) {
            System.out.println("post - if");
            // If the key equals to "image", we use FileBody to transfer the data
            entity.addPart( nameValuePairs.get(index).getName(), new FileBody(new File (nameValuePairs.get(index).getValue())));

        } else {
            System.out.println("post - else");
            // Normal string data
            entity.addPart(nameValuePairs.get(index).getName(), new StringBody(nameValuePairs.get(index).getValue()));
        }
    }
    System.out.println("post - done" + entity);

    httpPost.setEntity(entity);

    HttpResponse response = httpClient.execute(httpPost, localContext);

} catch (IOException e) {
    e.printStackTrace();
}
}

还有我的 php:

<?php

$uploads_dir = 'uploads/';

$uploadname = $_FILES["image"]["name"];
$uploadtitle = $_FILES["title"]["title"];


move_uploaded_file($_FILES['image']['tmp_name'], $uploads_dir.$uploadname);
file_put_contents($uploads_dir.'juhl.txt', print_r($uploadtitle, true));
?>

我一直在处理有关 MultipartEntity 的其他问题,但似乎找不到答案。我试过只发送Stringbody,但也没有任何成功。我认为问题出在服务器端(在 PHP 中),但欢迎提出任何建议。

这是我在这里的第一个问题-请随意评论形式和清晰度:-)

4

2 回答 2

0

试试这个方法

         ByteArrayBody bab1 = bab11;
         HttpClient httpClient = new DefaultHttpClient();
         httpPost = new HttpPost("link.php?api_name=api");
         MultipartEntity reqEntity = new MultipartEntity(
                HttpMultipartMode.BROWSER_COMPATIBLE);
         // this is for String
      try {
            reqEntity.addPart("udid", new StringBody(UDID));
          } 
       catch (Exception e) 
          {
          }
              // this is for image upload 
          try 
          {
                reqEntity.addPart("file1", bab1);   
          } catch (Exception e) 
          {
          }
                 // this is for video upload 
      try {             
                if (stPath1 != null) {
                    Log.e("path 1", stPath1);
                    Log.v("stDocType1", "video");
                    File file = new File(stPath1);
                    FileBody bin = new FileBody(file);
                    reqEntity.addPart("file1", bin);
                }
        } catch (Exception e) {
        }

           httpPost.setEntity(reqEntity);
           ResponseHandler<String> responseHandler = new BasicResponseHandler();
           response = httpClient.execute(httpPost, responseHandler);
于 2013-01-17T14:24:47.997 回答
0

问题是我的php。当您收到 Stringbody 时,只有一个参数(与文件体相反)。所以我删除了 $uploadtitle = $_FILES["title"]["title"]; 中的第二个参数 它奏效了

<?php
$uploads_dir = 'uploads/';

$uploadname = $_FILES["image"]["name"];
$uploadtitle = $_FILES["title"];


move_uploaded_file($_FILES['image']['tmp_name'], $uploads_dir.$uploadname);
file_put_contents($uploads_dir.'juhl.txt', print_r($uploadtitle, true));
?>

如果您遇到同样的问题,我希望这会有所帮助。

于 2013-01-22T11:08:23.703 回答