Android Multipart POST到Google App Engine无效

时间:2010-12-17 18:41:52

标签: java android google-app-engine http-post

我正在开发一个Android程序,它通过POST与App Engine程序共享数据,所有这些都在java中。当我使用MultipartEntity POST时,appengine程序由于某些我无法弄清楚的原因而无法看到我的任何POST变量!

Appengine Code:

public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    resp.setContentType("text/plain");
    resp.getWriter().println("Output:");

    String mRoutine = req.getParameter("Routine");
    resp.getWriter().println("Routine: " + mRoutine);

    String mPMode = req.getParameter("PMode");
    resp.getWriter().println("PMode: " + mPMode);
}

使用Android代码段

HttpPost post = new HttpPost("http://XXX.appspot.com/Echo");
HttpClient client = new DefaultHttpClient();
post.setEntity(new UrlEncodedFormEntity(data,HTTP.UTF_8));
HttpResponse response = client.execute(post);
HttpEntity resmarkMessagesReadFrom = response.getEntity();
if (resmarkMessagesReadFrom != null) {
    Result_STR= "Response:\n" + EntityUtils.toString(resmarkMessagesReadFrom);
    mMSGBox.setText(Result_STR);
}

Android代码段失败:

HttpPost post = new HttpPost("http://XXX.appspot.com/Echo");
HttpClient client = new DefaultHttpClient();
MultipartEntity reqmarkMessagesReadFrom = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
try {
    reqmarkMessagesReadFrom.addPart("Routine", new StringBody("Neutral"));
    post.setEntity(reqmarkMessagesReadFrom);
    HttpResponse response = client.execute(post);
    HttpEntity resmarkMessagesReadFrom = response.getEntity();
    if (resmarkMessagesReadFrom != null) {
        Result_STR= "Response:\n" + EntityUtils.toString(resmarkMessagesReadFrom);
        mMSGBox.setText(Result_STR);
    }

2 个答案:

答案 0 :(得分:3)

使用客户端上的MultipartEntity类,您需要以不同的服务器方式处理doPost,使用Apache Commons FileUpload等API读取文件/参数。

Google App Engine不允许写入文件系统,因此您需要使用Streaming API。

import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import java.io.InputStream;
..
public void doPost(HttpServletRequest req, HttpServletResponse res)
      throws ServletException, IOException {
    try {
      ServletFileUpload upload = new ServletFileUpload();
      res.setContentType("text/plain");

      FileItemIterator iterator = upload.getItemIterator(req);
      while (iterator.hasNext()) {
        String name = item.getFieldName();
        FileItemStream item = iterator.next();
        InputStream stream = item.openStream();

        if (item.isFormField()) {
          resp.getWriter().println((name + " : " + Streams.asString(stream));
        }
      }
    } catch (Exception ex) {
      throw new ServletException(ex);
    }
}

答案 1 :(得分:1)

我找到的最佳答案是google自己的一个例子:http://code.google.com/intl/fr/appengine/kb/java.html#fileforms这就像使用systemmpuntoout的例子一样使用FileUpload。

import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import java.io.InputStream;
import java.io.IOException;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FileUpload extends HttpServlet {
  private static final Logger log =
      Logger.getLogger(FileUpload.class.getName());

  public void doPost(HttpServletRequest req, HttpServletResponse res)
      throws ServletException, IOException {
    try {
      ServletFileUpload upload = new ServletFileUpload();
      res.setContentType("text/plain");

      FileItemIterator iterator = upload.getItemIterator(req);
      while (iterator.hasNext()) {
        FileItemStream item = iterator.next();
        InputStream stream = item.openStream();

        if (item.isFormField()) {
          log.warning("Got a form field: " + item.getFieldName());
        } else {
          log.warning("Got an uploaded file: " + item.getFieldName() + ", name = " + item.getName());

          // You now have the filename (item.getName() and the
          // contents (which you can read from stream).  Here we just
          // print them back out to the servlet output stream, but you
          // will probably want to do something more interesting (for
          // example, wrap them in a Blob and commit them to the
          // datastore).
          int len;
          byte[] buffer = new byte[8192];
          while ((len = stream.read(buffer, 0, buffer.length)) != -1) {
            res.getOutputStream().write(buffer, 0, len);
          }
        }
      }
    } catch (Exception ex) {
      throw new ServletException(ex);
    }
  }
}