3

我使用以下代码(取自 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,因为稍后我还必须发布图像。

谢谢。

4

3 回答 3

1

我认为问题不在客户端,而在服务器端。在第二个示例中,您发送的是 HTTP POST,但您发送的是 GET 等参数。

通常在您发送数据文件(即上传文件)时使用多部分请求,但在我看来,在您的代码中您没有发送任何数据文件,因此您不应该使用多部分请求。

于 2013-05-02T12:54:50.333 回答
0

整数到字符串的转换可能有问题。尝试这个:

reqEntity.addPart("lat", new StringBody(Integer.toString(lati)));
reqEntity.addPart("long", new StringBody(Integer.toString(longi)));

您可以使用lati + ""而不是Integer.toString(lati).

于 2013-05-04T13:05:18.890 回答
0

我测试了您的代码并使用 http 代理工具监视了 http 请求。有用。也许您的 php 代码中出现了一些错误。

class MyTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        testUpload();
        return null;
    }

}

public void testUpload() {  
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost("http://www.google.com/" + "ajax/process.AL.php");

    try {
        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("p", new StringBody("all"));

        httpPost.setEntity(reqEntity);
        httpClient.execute(httpPost);
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
} 
于 2013-04-30T12:42:52.570 回答