MultipartEntity and Json

I want to send an image and JsonObject to a 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 cannot read this in php because the format is similar:

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

I cannot read this in php due to backslashes. If I send json without image via httppost and bytearrayentity, there are no backslashes, and I have no problem.

+4
source share
2 answers

Use the following PHP:

 string stripslashes ( string $str ) 
+2
source

Perhaps you can just replace \" with \ using preg_replace on the PHP side

0
source

Source: https://habr.com/ru/post/1447165/


All Articles