在Android中发送多部分表单数据

时间:2013-04-21 07:24:56

标签: java android http multipartform-data

我使用下面的代码(取自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,因为我还需要稍后发布图片。

感谢。

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分: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();
    }
} 

答案 2 :(得分:0)

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

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

您可以使用lati + ""代替Integer.toString(lati)