java – MultipartEntity和Json

我想将图像和JsonObject发送到具有MultipartEntity的
PHP服务器.

这是我的代码:

HttpClient httpClient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(urlString);
File file = new File(imageend);
HttpResponse response = null;
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file, "image/jpeg");
StringBody sb;

try {
    sb = new StringBody(json.toString());

    mpEntity.addPart("foto", cbFile);
    mpEntity.addPart("json", sb);
    httppost.setEntity(mpEntity);
    response = httpClient.execute(httppost);

我无法在php中阅读这个,因为格式如下:

{\"test1\":\"Z\",\"test2\":\"1\"}

由于反斜杠,我无法在php中读取此内容.如果我通过httppost和bytearrayentity发布没有图像的json,则没有任何反斜杠,我没有问题.

最佳答案 使用以下PHP:

string stripslashes ( string $str )
点赞