我使用以下代码(取自 SO)将一些数据发布到 PHP 脚本:
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(Web.API_PREFIX_GENERAL + "ajax/process.AL.php");
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("mail", new StringBody("abcd@abcd.com"));
reqEntity.addPart("remote", new StringBody("1"));
reqEntity.addPart("altitle", new StringBody("GHI"));
reqEntity.addPart("aldesc", new StringBody("JKL"));
reqEntity.addPart("t", new StringBody("N"));
reqEntity.addPart("lat", new StringBody(lati));
reqEntity.addPart("long", new StringBody(longi));
reqEntity.addPart("p", new StringBody("all"));
httpPost.setEntity(reqEntity);
httpClient.execute(httpPost);
...这不会产生任何东西。没有错误,但也没有发布任何内容。
但是,如果我使用类似下面的东西并且没有添加任何部件reqEntity
,
String url = Web.API_PREFIX_GENERAL + "ajax/process.AL.php?mail=abcd@abcd.com&remote=1&altitle=GHI" +
"&aldesc=JKL&t=N&lat=" + lati + "&long=" + longi + "&p=all";
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
httpPost.setEntity(reqEntity);
httpClient.execute(httpPost);
...这行得通
我还设置了所有必需的 Apache 库。所以知道为什么会这样吗?
我需要使用MultipartEntity
,因为稍后我还必须发布图像。
谢谢。