我想發送一個圖像和一個JsonObject到一個帶有MultipartEntity的PHP服務器。 這裏是我的代碼:MultipartEntity和Json
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,則沒有任何反斜槓,我沒有問題。
你是如何創建'json'對象的preg_replace取代? – MKJParekh
JSONObject json = new JSONObject(); jQuery.put(「test1」,test1.getText()。toString());方法如下: json.put(「test2」,test2.getText()。toString()); – t18935
你能看看'json.toString()'返回什麼嗎?是否包含這些反斜槓? –