我正在開發一個Android程序,通過POST與App引擎程序共享數據,全部在java中。當我使用MultipartEntity進行POST時,appengine程序由於某些原因無法查看我的POST變量!Android多部門POST到谷歌應用程序引擎不工作
的AppEngine代碼:
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);
}
systempuntoout:感謝您的回覆。我一直無法讓FileUpload正常工作,所以我一直無法使用你的代碼。但是,我的示例AppEngine代碼使用簡單的html表單就可以獲得二進制上傳。我仍然認爲Android和AppEngine的溝通方式存在問題,我還沒有找到解決方法... – Robert 2011-01-06 17:16:51
@Robert你有沒有試過我的代碼片段? – systempuntoout 2011-01-06 17:29:05
不,我一直無法使FileUpload正常工作。我也擔心編譯器警告我FileUpload在應用程序加載時不會在服務器上可用... – Robert 2011-01-09 00:20:21