4

I want to send an image and a JsonObject to an PHP Server with MultipartEntity. Here is my Code:

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);

I can't read this in php because the format is like:

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

I'm not able to read this in php because of the backslashes. If I post json without image over httppost and bytearrayentity there aren't any backslashes and I have no problem.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
t18935
  • 59
  • 1
  • 6
  • 1
    how you are creating your `json` object? – MKJParekh Nov 20 '12 at 08:22
  • JSONObject json = new JSONObject(); try { json.put("test1", test1.getText().toString()); json.put("test2", test2.getText().toString()); – t18935 Nov 20 '12 at 08:34
  • Can you see what does `json.toString()` return? Does contain these backslashes? – Mikita Belahlazau Nov 20 '12 at 08:41
  • may be you should specifcy and encoding in your stringbody ? – njzk2 Nov 20 '12 at 08:53
  • @NikitaBeloglazov: Great question. I tried to String test = json.toString(); and there are no backslashes! So the problem should be the StringBody! But how can I read out the StringBody? I toggled a breakpoint but I can't see it. – t18935 Nov 20 '12 at 08:56
  • @njzk2 great idea how can I do this? – t18935 Nov 20 '12 at 08:56
  • in the stringbody constructor if i recall – njzk2 Nov 20 '12 at 08:57
  • btw, i used this method (json + multipart), (not in php though), and it did work, so i know it is possible to have a proper result. can you post the request dump (except the image full binary part) ? – njzk2 Nov 20 '12 at 08:58
  • I'm a Newbie how can I do this? I have to go away for a few minutes, will answer immediately after coming back. Thanks for your help!!! – t18935 Nov 20 '12 at 09:00
  • Now can you tell me how to post the request dump? – t18935 Nov 20 '12 at 10:25

2 Answers2

2

Use following PHP:

string stripslashes ( string $str )
bluish
  • 26,356
  • 27
  • 122
  • 180
Shyam
  • 6,376
  • 1
  • 24
  • 38
  • I tried this and it works, but if I want to upload an text with splashes in it it will delete them, right? – t18935 Nov 20 '12 at 08:30
  • then use /\ and remove slash – Shyam Nov 20 '12 at 08:40
  • if (get_magic_quotes_gpc()) { function stripslashes_deep($value) { $value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value); return $value; } $_POST = array_map('stripslashes_deep', $_POST); $_GET = array_map('stripslashes_deep', $_GET); $_COOKIE = array_map('stripslashes_deep', $_COOKIE); $_REQUEST = array_map('stripslashes_deep', $_REQUEST); } – Shyam Nov 20 '12 at 08:42
0

Maybe you can just replace the \" by \ using preg_replace on the PHP side

maddob
  • 989
  • 1
  • 12
  • 29
  • Ok that's a good idea. But isn't there any possibility to resolve it in Java that it doesn't set the splashes when creating StringBody? – t18935 Nov 20 '12 at 08:31
  • We had recently the same problem on an old server machine. The server automatically inserted the backslashes. The PHP version installed was 5.3.6. We could verify that on PHP versions 5.3.10 and higher the backslashes disappeared. The exact reason for this behavior is still unknown – maddob Jan 04 '13 at 10:34